useful recursive PHP functions

recursively ...

Recursive Directory Delete Function

Recursively delete whole directories with this function.

This PHP function scans a given directory and deletes all files and subdirectories it finds and has permission to delete.

Per default it removes the given directory totally, but you can specify to just "empty" it instead.
In this case it deletes everything inside the given directory and keeps the directory itself.

Since you can't delete non-empty directories with one of PHPs own functions,
here is the

remove directory recursively function:

download Recursive Directory Delete Function with comments

01 <?php
02
03 // ------------ lixlpixel recursive PHP functions -------------
04 // recursive_remove_directory( directory to delete, empty )
05 // expects path to directory and optional TRUE / FALSE to empty
06 // of course PHP has to have the rights to delete the directory
07 // you specify and all files and folders inside the directory
08 // ------------------------------------------------------------
09
10 // to use this function to totally remove a directory, write:
11 // recursive_remove_directory('path/to/directory/to/delete');
12
13 // to use this function to empty a directory, write:
14 // recursive_remove_directory('path/to/full_directory',TRUE);
15
16 function recursive_remove_directory($directory, $empty=FALSE)
17 {
18     // if the path has a slash at the end we remove it here
19     if(substr($directory,-1) == '/')
20     {
21         $directory = substr($directory,0,-1);
22     }
23  
24     // if the path is not valid or is not a directory ...
25     if(!file_exists($directory) || !is_dir($directory))
26     {
27         // ... we return false and exit the function
28         return FALSE;
29  
30     // ... if the path is not readable
31     }elseif(!is_readable($directory))
32     {
33         // ... we return false and exit the function
34         return FALSE;
35  
36     // ... else if the path is readable
37     }else{
38  
39         // we open the directory
40         $handle = opendir($directory);
41  
42         // and scan through the items inside
43         while (FALSE !== ($item = readdir($handle)))
44         {
45             // if the filepointer is not the current directory
46             // or the parent directory
47             if($item != '.' && $item != '..')
48             {
49                 // we build the new path to delete
50                 $path = $directory.'/'.$item;
51  
52                 // if the new path is a directory
53                 if(is_dir($path)) 
54                 {
55                     // we call this function with the new path
56                     recursive_remove_directory($path);
57  
58                 // if the new path is a file
59                 }else{
60                     // we remove the file
61                     unlink($path);
62                 }
63             }
64         }
65         // close the directory
66         closedir($handle);
67  
68         // if the option to empty is not set to true
69         if($empty == FALSE)
70         {
71             // try to delete the now empty directory
72             if(!rmdir($directory))
73             {
74                 // return false if not possible
75                 return FALSE;
76             }
77         }
78         // return success
79         return TRUE;
80     }
81 }
82 // ------------------------------------------------------------
83
84 ?>

learn more about the PHP functions used in this script:

substr( ), file_exists( ), is_dir( ), is_readable( ), opendir( ), readdir( ), unlink( ), closedir( ) and rmdir( ).

recursive directory delete function without comments:

download Recursive Directory Delete Function

01 <?php
02
03 // ------------ lixlpixel recursive PHP functions -------------
04 // recursive_remove_directory( directory to delete, empty )
05 // expects path to directory and optional TRUE / FALSE to empty
06 // ------------------------------------------------------------
07 function recursive_remove_directory($directory, $empty=FALSE)
08 {
09     if(substr($directory,-1) == '/')
10     {
11         $directory = substr($directory,0,-1);
12     }
13     if(!file_exists($directory) || !is_dir($directory))
14     {
15         return FALSE;
16     }elseif(is_readable($directory))
17     {
18         $handle = opendir($directory);
19         while (FALSE !== ($item = readdir($handle)))
20         {
21             if($item != '.' && $item != '..')
22             {
23                 $path = $directory.'/'.$item;
24                 if(is_dir($path)) 
25                 {
26                     recursive_remove_directory($path);
27                 }else{
28                     unlink($path);
29                 }
30             }
31         }
32         closedir($handle);
33         if($empty == FALSE)
34         {
35             if(!rmdir($directory))
36             {
37                 return FALSE;
38             }
39         }
40     }
41     return TRUE;
42 }
43 // ------------------------------------------------------------
44
45 ?>

top of page

© 2014 lixlpixel