php循环删除目录和文件函数

180it 2021-02-28 PM 1139℃ 0条
 //循环删除目录和文件函数
    public static function delDirAndFile($dirName, $delDir = 0)
    {
        if ($handle = opendir("$dirName"))
        {
            while (false !== ( $item = readdir($handle) ))
            {
                if ($item != "." && $item != "..")
                {
                    $file = str_replace('//', '/', "$dirName/$item");
                    if (is_dir($file))
                    {

                        self::delDirAndFile($file,$delDir);
                    } else
                    {
                        if (unlink($file))
                        {
//                            echo "成功删除文件: $file<br />\n";
                        }
                    }
                }
            }
            closedir($handle);
            if ($delDir == 1)
            {
                if (rmdir($dirName))
                    echo "成功删除目录: $dirName<br />\n";
            }
        }
    }

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

php循环删除目录和文件函数