PHP删除文件夹及文件夹下的所有文件

来源:互联网 发布:长沙网络推广公司排行 编辑:程序博客网 时间:2024/05/02 13:38

一.只删除文件夹包含的文件,不删除文件夹

[php] view plain copy
  1. public function deldir($dir) {  
  2.     //先删除目录下的文件:  
  3.     $dh = opendir($dir);  
  4.     while ($file = readdir($dh)) {  
  5.         if($file != "." && $file!="..") {  
  6.         $fullpath = $dir."/".$file;  
  7.         if(!is_dir($fullpath)) {  
  8.             unlink($fullpath);  
  9.         } else {  
  10.             deldir($fullpath);  
  11.         }  
  12.         }  
  13.     }  
  14.     closedir($dh);  
  15.       
  16. }  

二.删除文件夹及文件夹下所有的文件
[php] view plain copy
  1. public function deldir($dir) {  
  2.     //先删除目录下的文件:  
  3.     $dh = opendir($dir);  
  4.     while ($file = readdir($dh)) {  
  5.         if($file != "." && $file!="..") {  
  6.         $fullpath = $dir."/".$file;  
  7.         if(!is_dir($fullpath)) {  
  8.             unlink($fullpath);  
  9.         } else {  
  10.             deldir($fullpath);  
  11.         }  
  12.         }  
  13.     }  
  14.     closedir($dh);  
  15.       
  16.     //删除当前文件夹:  
  17.     if(rmdir($dir)) {  
  18.         return true;  
  19.     } else {  
  20.         return false;  
  21.     }  
  22. }  

三.创建文件夹并指定权限和编码
[php] view plain copy
  1. if (!is_dir($dir)){                                 //如果目录不存在  
  2.     mkdir(iconv("UTF-8""GBK"$dir),0777,true);   //创建目录,777权限,GBK编码格式  

阅读全文
0 0
原创粉丝点击