Home >Backend Development >PHP Tutorial >PHP recursively deletes directories_PHP tutorial
Excerpted from “linuxer” blog http://deidara.blog.51cto.com/400447/118805
Deleting an empty directory is very simple ~ just use the
rmdir() function. However, if you want to delete a non-empty directory, you will not be able to delete it quickly. You must first delete the files in the directory. But there may be subdirectories in the directory, so recursive deletion is required~ Here is my example~
function deletedir($dir){
if(!handle=@opendir($dir)){ //Check whether the directory to be opened exists
die(" No directory ")); <">}
While (FALSE! == ($ File = Readdir ($ handle)) {
if ($ file! == "." && $ file! == ".."){ //Exclude the current directory and parent directory
🎜> deletedir($file);
echo "File$file was deleted successfully.
";
echo "file & lt; b & gt; $ file & lt;/b & gt; delete failure!"
with with with with with with with echo "Directory$dir was deleted successfully.
";
} Else {
Echo" Directory & lt; b & gt; $ dir & lt;/b & gt; delete failure!
";
}
}
//Test program
$dir="/var/www/test";
deletedir($dir);
?>
Create a folder and file test under the /var/www/test folder
shell> touch aaa
shell> touch bbb
shell> touch ccc
shell> touch eee
shell> touch ffff
shell> mkdir 111
shell> mkdir 333
Create and write files in the 111, 222, 333 folders respectively. Say more, then give them permission
shell>chown [url]www.www[/url] test -R
Then open the program in IE and test it~~ Haha.
http://www.bkjia.com/PHPjc/508519.html
www.bkjia.com
true