精美而实用的网站,关注web编程技术、网站运营、SEO推广,让您轻松愉快的学习

PHP无限级删除目录及文件,有多少级的目录都可以遍历,且删除目录内的文件,典型的PHP FSO操作示例,用到的PHP函数也是较多,像file_exists、opendir、readdir、unlink等,本代码对熟悉上述函数的用法也是有帮助的:

<?php
//PHP删除目录
class  del_path
{
    function  wm_chief_delpath($del_path)
    {
        if(!file_exists($del_path))//若目录不存在则建立
        {
            echo"操作有误:目录不存在!";return false;}
            $hand=opendir($del_path);
            $i=0;
            while($file=readdir($hand))
            {
                $i++;
                if($i==1||$i==2)
                {continue;}
                if(!(strchr($file,".")))
                {
                    $del_s_path=$del_path."/".$file;
                    $this->wm_chief_delpath($del_s_path);
                }else{
                    $del_file=$del_path."/".$file;
                    $this->wm_chief_file($del_file);
                }
            }
        closedir($hand);
        $this->wm_chief_path($del_path);
        return  true;
        }
        //删除文件
        function  wm_chief_file($del_file)
        {
        unlink($del_file);
        }
        //删除目录
        function  wm_chief_path($del_path)
        {
        rmdir($del_path);
        }
        }
        $DelPath="upload";//要删除的目录
        $wm_chief=new  del_path();
        $wm_chief_ok=$wm_chief->wm_chief_delpath($DelPath);
        if($wm_chief_ok)
        {
    echo"删除完毕";
}
?>

有些地方的代码没有经过整理,时间关系,用的朋友自己整理吧。

Tags:php 删除文件