Home > php教程 > PHP源码 > body text

php 删除文件夹(可删除不是空的文件夹)

WBOY
Release: 2016-06-08 17:26:17
Original
1284 people have browsed it

这是一款删除文件夹的php函数,本函数用的是递归删除,他可以删除指定目录的所有文件夹与文件,可以删除不是空目录的文件夹,方便的很,下面来看看函数实例方法。

<script>ec(2);</script>
 代码如下 复制代码
/*—————————————————— */
//– 递归删除文件及目录
//– 例: del_dir ('../www.1111cn.net/');注意:返回的/是必须的
//– $type 强制删除目录, true 是 ,false 否
/*—————————————————— */
function del_dir ($dir,$type=true)
{
$n=0;
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
//.svn 忽略 svn 版本控制信息
if ( $file == '.' or $file =='..' or $file == '.svn')
{
continue;
}
if (is_file ($dir.$file))
{
unlink($dir.$file);
$n++;
}
if (is_dir ($dir.$file))
{
del_dir ($dir.$file.'/');
if ($type)
{
$n++;
rmdir($dir.$file.'/');
}
}
}
}
closedir($dh);
}
return $n;
}

 很容易看懂吧。原理很简单,首先是查询目录所有文件或目录,如果是文件就删除了,如果是目录看看是不是空目录,如果是的删除,否则再调用本函数一次类推,就实例了删除不是空目录的功能了。

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!