php method to delete multi-level directories: first create a PHP sample file; then define a rmdirs method; then pass "while (false!==($child=$d->read())) {..}" method to clear all files in the directory; finally call the rmdirs method to delete.
Recommendation: "PHP Video Tutorial"
Previously, due to project requirements, it was necessary to use php to create a multi-level directory, then Once created, it must be deleted. We all know that the functions that come with the PHP system include functions for deleting directories, such as rmdir(). The syntax prototype of this function is such as: rmdir(dir,context), which means trying to delete the directory specified by dir. Directory.
But the directory dir must be empty. There is a problem. When there is content in the directory or there are directories in the directory, when we determine that the directory and all the files and directories in the directory have no meaning, we need to delete the directory. How to do it?
The simple rmdir() function can no longer meet our needs, so the following function was born. The following function will directly delete the directory you need to specify to delete. There are Files or multi-level directories will be killed mercilessly by it. Haha, just go to the code.
function rmdirs($dir){ $d=dir($dir); while (false!==($child=$d->read())){//这部分代码是清除目录里面的所有文件的 if($child!='.'&&$child!='..'){ if(is_dir($dir.'/'.$child)){ rmdirs($dir.'/'.$child); }else{ unlink($dir.'/'.$child); } } } $d->close(); rmdir($dir);//清除目录 }
Articles you may be interested in
The above is the detailed content of How to delete multi-level directories in php. For more information, please follow other related articles on the PHP Chinese website!