php删除文件函数详解
以下是代码片段: function del_file($path){ if (file_exists($path)){ if(is_file($path)){ if( !@unlink($path) ){ $show.="$path,"; } } else{ $handle = opendir($path); while (($file = readdir($handle))!='') { if (($file!=".") && ($file!="..") && ($file!="")){ if (is_dir("$path/$file")){ $show.=del_file("$path/$file"); } else{ if( !@unlink("$path/$file") ){ $show.="$path/$file,"; } } } } closedir($handle);
if(!@rmdir($path)){ $show.="$path,"; } } } return $show; } |
上面这个是删除文件的函数,使用频率也是很高的,大家都知道,使用系统自身的函数只能是删除单个文件,而无法递归的删除多级目录与文件,而这个函数正好解决了这个问题,不仅仅可以删除单个文件,也可以删除多级目录。比如整站数据生成静态后,就会生成很多文件与很多目录。使用这个函数的话,就可以轻易的把某个目录下的所有文件一下子全部删除。
|