Home  >  Article  >  php教程  >  递归删除指定目录下的非空目录及文件

递归删除指定目录下的非空目录及文件

PHP中文网
PHP中文网Original
2016-05-25 16:58:13936browse

php代码

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);
function deldir($dirpath){
    if(!file_exists($dirpath)){
        exit(&#39;Nothing is about that!&#39;);
    }

    $handle=opendir($dirpath);
    while($filename=readdir($handle)){
        //排除系统文件中的‘.’特殊文件
        if($filename == &#39;.&#39; || $filename == &#39;..&#39;){
            continue;
        }
        $filepath=$dirpath.&#39;/&#39;.$filename;
        echo $filepath.&#39;<br>&#39;;

        //删除文件
        if(is_file($filepath)){
            unlink($filepath);
        }
        //删除文件及递归删除非空目录下的文件
        if(is_dir($filepath)){
            deldir($filepath);
        }
    }
    echo &#39;恭喜你!你成功删除了以上文件:<br>&#39;;
    closedir($handle);
    //删除目录
    rmdir($dirpath);
}
//执行路径文件夹
deldir(&#39;./path&#39;);
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:查询字串解析Next article:php 简单计算器