#php method to delete a directory and files under the directory:
First create a new php file "test.php" and add the following code:
<? function deldir($dir) { //先删除目录下的文件: $dh=opendir($dir); while ($file=readdir($dh)) { if($file!="." && $file!="..") { $fullpath=$dir."/".$file; if(!is_dir($fullpath)) { unlink($fullpath); } else { deldir($fullpath); } } } closedir($dh); //删除当前文件夹: if(rmdir($dir)) { return true; } else { return false; } } ?>
Then create the folder "test" in the same directory as test.php, and create subfiles or directories in its directory.
j Then add some code to "test.php" to delete the file:
function deldir(test);
Finally check whether the "test" directory has been deleted.
Related references:php中文网
The above is the detailed content of How to delete a directory and files in the directory in php. For more information, please follow other related articles on the PHP Chinese website!