php自定义函数之递归删除文件及目录

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

/*—————————————————— */
//– 递归删除文件及目录
//– 例: del_dir (‘../cache/');注意:返回的/是必须的
//– $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;
}

相关文章

php 无限级 SelectTree 类

复制代码 代码如下:/* author: nick date: 2009.05.17 功能:生成SeletTree 属性: $result 结果集 $id_field 自身id字段 $p...

如何在PHP中使用正则表达式进行查找替换

1. preg_match — 执行一个正则表达式匹配int preg_match ( string $pattern , string $subject [, array &$matc...

php异常处理方法实例汇总

本文实例讲述了php异常处理方法。分享给大家供大家参考。具体如下: <?php $path = "D://in.txt"; try //检测异常 { fil...

php实现文件上传及头像预览功能

php实现文件上传及头像预览功能

php文件上传原理是通过form表单的enctype="multipart/form-data"属性将文件临时放到wamp文件夹中的tmp目录下,再通过后台php程序将文件保存在体统中。...

PHP调用全国天气预报数据接口查询天气示例

本文实例讲述了PHP调用全国天气预报数据接口查询天气。分享给大家供大家参考,具体如下: 基于PHP的聚合数据全国天气预报API服务请求的代码样例 本代码示例是基于PHP的聚合数据全国天气...