Home > php教程 > php手册 > PHP删除指定目录下的所有文件

PHP删除指定目录下的所有文件

WBOY
Release: 2016-06-21 09:06:01
Original
1114 people have browsed it

/**
 * 删除指定目录下的所有文件
 *
 * @param String $dir  要进行操作的路径
 * 适合范围,只有用于文件夹内不存在子文件夹的情况下
 * 来源  DZ
 * 小佳(www.phpcina.cn)  整理 于 2006-06-26 
 */
function dir_clear($dir) {
    $directory = dir($dir);                //创建一个dir类(PHP手册上这么说的),用来读取目录中的每一个文件
    while($entry = $directory->read()) {   //循环每一个文件,并取得文件名$entry
        $filename = $dir.'/'.$entry;       //取得完整的文件名,带路径的
        if(is_file($filename)) {           //如果是文件,则执行删除操作
            @unlink($filename);
        }
    }
    $directory->close();                   //关闭读取目录文件的类
    result();
}  

 



Related labels:
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