加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php 删除文件夹,及其其下所有文件

发布时间:2020-12-13 20:52:54 所属栏目:PHP教程 来源:网络整理
导读:在php中删除文件和删除目录我们都用unlink删除实现删除,如果要删除不是空目录主要利用readdir和opendir来遍历目录了. 实例代码如下: ?php //删除文件夹,及其其下所有文件 function deldir( $dir ){ $dh =opendir( $dir ); while ( $file =readdir( $dh )){ i

在php中删除文件和删除目录我们都用unlink删除实现删除,如果要删除不是空目录主要利用readdir和opendir来遍历目录了.

实例代码如下:

  1. <?php 
  2. // 删除文件夹,及其其下所有文件 
  3. function deldir($dir) { 
  4.   $dh=opendir($dir); 
  5.    
  6.   while ($file=readdir($dh)) { 
  7.    
  8.     if($file!="." && $file!="..") { 
  9.      
  10.     $fullpath=$dir."/".$file
  11.      
  12.       if(!is_dir($fullpath)) { 
  13.        
  14.         unlink($fullpath); 
  15.        
  16.       } else { 
  17.        
  18.         deldir($fullpath); 
  19.        
  20.       } 
  21.      
  22.     } 
  23.    
  24.   } 
  25.    
  26.   closedir($dh); 
  27.    
  28.   if(rmdir($dir)) { 
  29.    
  30.     return true; 
  31.    
  32.   } else { 
  33.    
  34.     return false; 
  35.    
  36.   } 
  37. ?> 

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读