php清空(删除)指定目录下的文件,不删除目录文件夹的实现代码_PHP

WBOY
Release: 2016-05-31 19:29:51
Original
821 people have browsed it

web 开发中我们可能会碰到需要将某个目录下的所有文件清空,但不删除这个目录下的子目录(当然也不会删除被删除的根目录)的情况。那么对于这种只删除文件而不删除目录的方法,你是如何处理的呢?下面博主将要和你分享一下对于这个问题的一种比较好的解决办法。看下面这个函数:

/*删除指定目录下的文件,不删除目录文件夹*/
function delFile($dirName){
	if(file_exists($dirName) && $handle=opendir($dirName)){
		while(false!==($item = readdir($handle))){
			if($item!= "." && $item != ".."){
				if(file_exists($dirName.'/'.$item) && is_dir($dirName.'/'.$item)){
					delFile($dirName.'/'.$item);
				}else{
					if(unlink($dirName.'/'.$item)){
						return true;
					}
				}
			}
		}
		closedir( $handle);
	}
}
Copy after login

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!