php代码删除非空目录的方法:1、创建一个PHP示例文件;2、设置文件编码为utf8;3、通过递归函数实现删除非空目录即可,其代码如“function deldir($dir){if(file_exists($dir)){$files=scandir($dir);foreach($files as $file){...}}”。

本教程操作环境:windows7系统、PHP8.1版、Dell G3电脑。

php代码怎么删除非空目录?

代码如下:

<?php
header("Content-type: text/html; charset=utf-8");
 
$dir='mydir';
 
function deldir($dir){
    if(file_exists($dir)){
          $files=scandir($dir);
          foreach($files as $file){
                 if($file!='.' && $file!='..'){
                         $path=$dir.'/'.$file;
                         if(is_dir($path)){
                                 deldir($path);
                          }else{
                                  unlink($path);
                          }
                  }
           }
           rmdir($dir);
           return true;
    }else{
           return false;
    }
}
 
if(deldir($dir)){
      echo "目录删除成功!";
 }else{
      echo "没有目录!";
 }

php的非空目录删除,其实是用递归函数实现的,php没有直接删除非空目录的函数。


php代码怎么删除非空目录