PHP removal guide that is not empty

Possible duplicate:
how to delete content folder using php

I know that you can delete a folder empty with rmdir. And I know that you can clear the folder with the following three lines.

foreach($directory_path as $file) { unlink($file); } 

But what if one of the files is actually a subdirectory. How can you get rid of this, but in infinite quantities, like the effect of a double mirror. Is there any delete command in php?

thanks

+4
source share
3 answers

This function will recursively delete the directory:

 function rmdir_recursive($dir) { foreach(scandir($dir) as $file) { if ('.' === $file || '..' === $file) continue; if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file"); else unlink("$dir/$file"); } rmdir($dir); } 

This one too:

 function rmdir_recursive($dir) { $it = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS); $it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST); foreach($it as $file) { if ($file->isDir()) rmdir($file->getPathname()); else unlink($file->getPathname()); } rmdir($dir); } 
+34
source

From the PHP rmdir page:

 <?php function rrmdir($dir) { if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != "..") { if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); } } reset($objects); rmdir($dir); } } ?> 

and

 <?php function delTree($dir) { $files = glob( $dir . '*', GLOB_MARK ); foreach( $files as $file ){ if( substr( $file, -1 ) == '/' ) delTree( $file ); else unlink( $file ); } if (is_dir($dir)) rmdir( $dir ); } ?> 
+7
source
 <?php function rrmdir($dir) { if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != "..") { if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); } } reset($objects); rmdir($dir); } } ?> 

from PHP documentation

+2
source

All Articles