Home > Article > Backend Development > How to delete folder contents in php
php method to delete the contents of a folder: first create a PHP sample file; then define a deldir method; then open the file directory through the opendir function; finally delete the files and empty folders in the directory recursively. Can.
Recommended: "PHP Video Tutorial"
phpDelete the folder and its contents
The code is as follows:
<?php 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; } }
The above is the detailed content of How to delete folder contents in php. For more information, please follow other related articles on the PHP Chinese website!