Home  >  Article  >  php教程  >  删除目录及目录下文件

删除目录及目录下文件

PHP中文网
PHP中文网Original
2016-05-25 17:06:59896browse

php代码

/**
  +-----------------------------------------------------------------------------------------
 * 删除目录及目录下所有文件或删除指定文件
  +-----------------------------------------------------------------------------------------
 * @param str $path   待删除目录路径
 * @param int $delDir 是否删除目录,1或true删除目录,0或false则只删除文件保留目录(包含子目录)
  +-----------------------------------------------------------------------------------------
 * @return bool 返回删除状态
  +-----------------------------------------------------------------------------------------
 */
function delDirAndFile($path, $delDir = FALSE) {
    if ($handle = opendir("$path")) {
        while (false !== ( $item = readdir($handle) )) {
            if ($item != "." && $item != "..")
                is_dir("$path/$item") ? delDirAndFile("$path/$item", $delDir) : unlink("$path/$item");
        }
        closedir($handle);
       //http://www.tongqiong.com
        if ($delDir)
            return rmdir($path);
    }else {
        return unlink($path);
    }
}
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