Home > php教程 > PHP源码 > body text

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

PHP中文网
Release: 2016-05-25 16:58:13
Original
898 people have browsed it

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;);
?>
Copy after login
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!