Discuz教程网

rrmdir php中递归删除目录及目录下的文件

[复制链接]
authicon 星火燎原 发表于 2011-5-17 22:12:22 | 显示全部楼层 |阅读模式
php自带的rmdir,只能删除空目录,这个rrmdir就可以递归删除目录及目录下的所有文件,不过使用起来要小心哦,不要把所有文件都删了




代码如下:


  1. function rrmdir($dir) {
  2. if (is_dir($dir)) {
  3. $objects = scandir($dir);
  4. foreach ($objects as $object) {
  5. if ($object != “.” && $object != “..”) {
  6. if (filetype($dir.”/”.$object) == “dir”) rrmdir($dir.”/”.$object); else unlink($dir.”/”.$object);
  7. }
  8. }
  9. reset($objects);
  10. }
  11. }

复制代码

rmdir
(PHP 4, PHP 5)
rmdir — 删除目录
Report a bug 说明
bool rmdir ( string $dirname )
尝试删除 dirname 所指定的目录。 该目录必须是空的,而且要有相应的权限。成功时返回 TRUE, 或者在失败时返回 FALSE.
Note: 自 PHP 5.0.0 起 rmdir() 也可用于某些 URL 封装协议。参见Supported Protocols and Wrappers 的列表看看 rmdir() 支持哪些 URL 封装协议。
Note: 在 PHP 5.0.0 中增加了 对上下文(Context)的支持。有关 上下文(Context) 的说明参见 Stream 函数。
Note: 当启用 安全模式时, PHP 会在执行脚本时检查被脚本操作的目录是否与被执行的脚本有相同的 UID(所有者)。
参见 mkdir() 和 unlink()。

代码如下:

  1. <?php
  2. function rrmdir($dir) {
  3. if (is_dir($dir)) {
  4. $objects = scandir($dir);
  5. foreach ($objects as $object) {
  6. if ($object != "." && $object != "..") {
  7. if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
  8. }
  9. }
  10. reset($objects);
  11. rmdir($dir);
  12. }
  13. }
  14. ?>
复制代码




This isn't my code, but just thought I would share, since it took me so long to find. This is a function to delete a folder, all sub-folders, and files in one clean move.
Just tell it what directory you want deleted, in relation to the page that this function is executed. Then set $empty = true if you want the folder just emptied, but not deleted. If you set $empty = false, or just simply leave it out, the given directory will be deleted, as well.
代码如下:


  1. <?php
  2. function deleteAll($directory, $empty = false) {
  3. if(substr($directory,-1) == "/") {
  4. $directory = substr($directory,0,-1);
  5. }
  6. if(!file_exists($directory) || !is_dir($directory)) {
  7. return false;
  8. } elseif(!is_readable($directory)) {
  9. return false;
  10. } else {
  11. $directoryHandle = opendir($directory);
  12. while ($contents = readdir($directoryHandle)) {
  13. if($contents != '.' && $contents != '..') {
  14. $path = $directory . "/" . $contents;
  15. if(is_dir($path)) {
  16. deleteAll($path);
  17. } else {
  18. unlink($path);
  19. }
  20. }
  21. }
  22. closedir($directoryHandle);
  23. if($empty == false) {
  24. if(!rmdir($directory)) {
  25. return false;
  26. }
  27. }
  28. return true;
  29. }
  30. }
  31. ?>
复制代码



A patch to previous script to make sure rights for deletion is set:
代码如下:

  1. <?php
  2. //Delete folder function
  3. function deleteDirectory($dir) {
  4. if (!file_exists($dir)) return true;
  5. if (!is_dir($dir) || is_link($dir)) return unlink($dir);
  6. foreach (scandir($dir) as $item) {
  7. if ($item == '.' || $item == '..') continue;
  8. if (!deleteDirectory($dir . "/" . $item)) {
  9. chmod($dir . "/" . $item, 0777);
  10. if (!deleteDirectory($dir . "/" . $item)) return false;
  11. };
  12. }
  13. return rmdir($dir);
  14. }
  15. ?>
复制代码






上一篇:首页多个求助
下一篇:php中在PDO中使用事务
authicon kurt226 发表于 2011-5-18 05:30:01 | 显示全部楼层
这个还是不错的!
authicon mjz 发表于 2011-5-19 05:30:03 | 显示全部楼层
有意思~顶顶 ,继续顶顶。继续顶哦
authicon nancybingling 发表于 2011-5-19 11:30:02 | 显示全部楼层
楼主真强大
authicon fantuanzi 发表于 2011-5-19 18:31:12 | 显示全部楼层
支持楼主,顶一下
authicon lilac_yao 发表于 2011-5-20 15:30:07 | 显示全部楼层
哦哦,发财了啊,看到好东西啦
authicon №小乖 发表于 2011-5-21 10:53:19 | 显示全部楼层
看一下啊,嘻嘻
authicon shakesxia 发表于 2011-5-22 18:59:34 | 显示全部楼层
顶顶更健康
authicon 纤陌陌 发表于 2011-6-19 16:59:32 | 显示全部楼层
支持!好东西,拿走了!
authicon yuki佳 发表于 2011-6-20 04:01:01 | 显示全部楼层
偶的天啊!爱死你了
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 16:26

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表