# Solved: PHP Unlink() Help



## Xperience (Feb 13, 2007)

Hi 

What i need is some functionality for me and my other admins to delete files on our server.
The page has been setup to display the files on the server, but now we just need as of above.

So next to each file name, maybe a checkbox and then admins check the boxes in relation to the files they want to delete. Then they click a submit button and the files are deleted.

Let your mind go wild. I am willing to complete any procedure you supply me


----------



## Xperience (Feb 13, 2007)

Bump


----------



## brendandonhu (Jul 8, 2002)

You can use glob() to create a list of files. In your form, you can name each checkbox something like "FilesList[]". PHP will get an array of the files you selected, and you can use a foreach() loop to delete each one.


----------



## Xperience (Feb 13, 2007)

Hi 

Umm i really dont understand sorry lol  I already have the list of files created. What do i put for form action? And if its a something.php file, what goes in that file?

Thanks


----------



## brendandonhu (Jul 8, 2002)

Your forms action is where you want the results to go. You might want to take a look at the manual at php.net if you don't know how to write any of it.


----------



## Xperience (Feb 13, 2007)

okay, but how do i delete the files? what code do i need? could you possibly supply a sample code and then i may be able to go from there?


----------



## MMJ (Oct 15, 2006)

Do you know how to make a checkbox or radio button form?


----------



## Xperience (Feb 13, 2007)

I certainly do. I just need php code


----------



## brendandonhu (Jul 8, 2002)

To delete a file, you do unlink('filename');. If you could post what code you have or what you're stuck on, it might be easier to help.


----------



## Xperience (Feb 13, 2007)

Okay, im stuck on how i can select multiple files for deletion, and then click a button and they delete.


----------



## brendandonhu (Jul 8, 2002)

Take a look at post #3. If you name your checkboxes like FilesToDelete[], it will pass an array of the files you select. Then you can run unlink() on each one to delete it.


----------



## Xperience (Feb 13, 2007)

awesome! so then the unlink code will go in delete.php for example and form action will be "delete.php"? If so, great! And then what do i use for filename in unlink() function? (i.e the name between the brackets).

And with the FilesToDelete[], in between the braces, do i put a number? For example:


```

```
Or do i name each checkbox just FilesToDelete[] with no number in the braces?


----------



## brendandonhu (Jul 8, 2002)

That's correct. You need to use a loop to delete each file:

```
foreach($_POST['FileToDelete'] as $filename)
{
  unlink($filename);
}
```


----------



## Xperience (Feb 13, 2007)

cool. so that code will go in delete.php? And then i dont put numbers in the braces?


----------



## Xperience (Feb 13, 2007)

Alrighty  Seems to be going fine except:

Warning: unlink(/www/110mb.com/t/o/m/m/o/8/8/8/tommo888/htdocs/on) [function.unlink]: No such file or directory in /www/110mb.com/t/o/m/m/o/8/8/8/tommo888/htdocs/admin/delete.php on line 5

Why "on" instead of the file i want to delete?


----------



## Xperience (Feb 13, 2007)

Sorry for triple post, but yay! It works! I simply just added a hidden input and then refrenced it in the php file. Works like a charm! Thanks for the help guys


----------



## brendandonhu (Jul 8, 2002)

[tsg=yourewelcome]yourewelcome[/tsg]


----------

