hi,欢迎访问本站!
当前位置: 首页编程开发正文

php删除文件夹的方法

墨初 编程开发 287阅读

利用一个php脚本去删除服务器上一个文件夹,需要先将文件夹内的所有文件先删除,然后再删除其文件,下面给出两个php删除文件夹的方法,供参考。

php删除文件夹的方法

方法1:

php删除文件夹以及文件夹下所有的文件。

/**
 * # php删除一个指定的文件夹
 * @param string $dir 需要删除的文件夹
 * 
 * @return bool 
 * @host https:www.73so.com
 */
function deldir($dir) 
{
    //先删除目录下的文件:
    $dh=opendir($dir);
    while ($file=readdir($dh)) {
        if($file!="." && $file!="..") {
        $fullpath=$dir."/".$file;
        if(!is_dir($fullpath)) {
            unlink($fullpath);
        } else {
            deldir($fullpath);
        }
        }
    }
    closedir($dh);
    //删除当前文件夹:
    if(rmdir($dir)) {
        return true;
    } else {
        return false;
    }
}

方法2:

删除文件夹下指定的文件,下面的示例是删除指定文件夹下的mochu文件以及其文件夹下的所有文件。

function delsvn($dir) 
{
    $dh=opendir($dir);
    //找出所有".svn“ 的文件夹:
    while ($file=readdir($dh)) {
        if($file!="." && $file!="..") {
            $fullpath=$dir."/".$file;
            if(is_dir($fullpath)) {
                if($file=="mochu"){
                    delsvndir($fullpath);
                }else{
                    delsvn($fullpath);
                }
            }
        }
    }
    closedir($dh);
}
function delsvndir($svndir)
{
    //先删除目录下的文件:
    $dh=opendir($svndir);
    while($file=readdir($dh)){
        if($file!="."&&$file!=".."){
            $fullpath=$svndir."/".$file;
            if(is_dir($fullpath)){
                delsvndir($fullpath);
            }else{
                unlink($fullpath);
            }
        }
    }
    closedir($dh);
    //删除目录文件夹
    if(rmdir($svndir)){
        return  true;
    }else{
        return false;
    }
}

以上就是关于php删除文件夹的方法,更多关于php的教程可参考本博客的其它文章。

标签:
声明:无特别说明,转载请标明本文来源!
相关推荐