How to delete a file directory in php?
php method to delete a file directory:
php traverses all files and folders in a folder and deletes all folders and subfolders The code for all files is implemented recursively to achieve the effect of clearing a directory. The code is simple and practical.
is also suitable for clearing the cache in thinkphp. In thinkphp, you can write the following code into the ./Application/Admin/Common/function.php file, and then call this function in the controller to perform the cleaning operation.
Function used:
scandir($path) Traverse all files in a folder and return an array.
unlink($filename) Delete the file.
rmdir($path) Delete only empty folders
<?php //设置需要删除的文件夹 $path = "./Application/Runtime/"; //清空文件夹函数和清空文件夹后删除空文件夹函数的处理 function deldir($path){ //如果是目录则继续 if(is_dir($path)){ //扫描一个文件夹内的所有文件夹和文件并返回数组 $p = scandir($path); foreach($p as $val){ //排除目录中的.和.. if($val !="." && $val !=".."){ //如果是目录则递归子目录,继续操作 if(is_dir($path.$val)){ //子目录中操作删除文件夹和文件 deldir($path.$val.'/'); //目录清空后删除空文件夹 @rmdir($path.$val.'/'); }else{ //如果是文件直接删除 unlink($path.$val); } } } } } //调用函数,传入路径 deldir($path);
Recommended tutorial: "PHP Video Tutorial"
The above is the detailed content of How to delete a file directory in php. For more information, please follow other related articles on the PHP Chinese website!