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实现递归复制整个文件夹的类实例

本文实例讲述了PHP实现递归复制整个文件夹的类。分享给大家供大家参考。具体如下: <?php /* * 文件夹复制类 */ class CopyFile { pub...

php 友好URL的实现(吐血推荐)

友好URL的实现(吐血推荐) 大家经常看到别的站的URL是这样的吧? http://www.xxx.com/module/show/action/list/page/7 或者 http:...

php实现子字符串位置相互对调互换的方法 原创

本文实例讲述了php实现子字符串位置相互对调互换的方法。分享给大家供大家参考,具体如下: <?php /*子字符串位置互换 */ $str1="Tom"; $str2...

php实现通过cookie换肤的方法

本文实例讲述了php实现通过cookie换肤的方法。分享给大家供大家参考。具体如下: saveStyleSheet.php页面如下: <?php function s...

关于PHP中Object对象的笔记分享

1.当将所有实例设为null,php会自动清除对象的引用。 2.建构子:__construct() 清除对象时自动执行的方法:__destruct() 也可以设置手动清除对象的方法:de...