Php Delete

8.07.2007 | Old Posts

I’ve decided its about time to share some of my knowledge with allof you so Im openeing a new category “Tutorials”. I hope this will come in useful to many people. So here it goes.

Php Delete
This is a file I wrote a while ago and has come in useful several times. When deleting files on a remote server ftp is the obvious way. But what if you don’t want to ftp delete your files? What if you want to delete files and folders say in an admin section of a website? Well here is the script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?
//Usage: recursive_remove_directory("this/is/a/dir");
function recursive_remove_directory($directory, $empty=FALSE){
     // if the path has a slash at the end we remove it here
     if(substr($directory,-1) == '/') {
         $directory = substr($directory,0,-1);
     }
 
     // if the path is not valid or is not a directory ...
     if(!file_exists($directory) || !is_dir($directory)) {
         return FALSE;
     }
     elseif(!is_readable($directory)) {  // ... if the path is not readable
         return FALSE;
     } else { // ... else if the path is readable
         $handle = opendir($directory); // we open the directory
         // and scan through the items inside
         while (FALSE !== ($item = readdir($handle))) {
             // if the filepointer is not the current directory or the parent directory
             if($item != '.' &amp;&amp; $item != '..') {
                 $path = $directory.'/'.$item; // we build the new path to delete
                 if(is_dir($path)) { // if the new path is a directory
                     recursive_remove_directory($path); // we call this function with the new path
                 } else { // if the new path is a file
                     unlink($path); // we remove the file
                     echo"Removed: $path"; //Output whats going on
                 }
             }
         }
         closedir($handle); // close the directory
 
         // if the option to empty is not set to true
         if($empty == FALSE) {
             if(!rmdir($directory)) { // try to delete the now empty directory
                 return FALSE;
             }
         }
 
         return TRUE; // return success
     }
}
?>

So as you can see this recursively deletes all the files in a directory and then, if set, deletes the directory as well. I use a HTML form to make it easy to select which directory you want to delete.

Please select a folder:<br />
<form action="phpDelete.php" method="post"><br />
<input id="dir" name="dir" type="text" /><br />
<input value="Delete" type="submit" /><br />
</form>

WARNING: This is NOT safe to have just lying around on your server. If someone finds this file they could delete whatever they want. Please password protect this file or have it in a secure part of your website. Preferrably delete this file form your server if you don’t need it anymore.

Get the source code here: phpDelete.txt

  • Digg
  • StumbleUpon
  • del.icio.us
  • Reddit
  • Wikio
  • Technorati
  • Pownce
  • E-mail this story to a friend!

Similar Posts

Comments