当前位置:Gxlcms > PHP教程 > php递归删除目录及文件的自定义函数rrmdir

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

时间:2021-07-01 10:21:17 帮助过:12人阅读

  1. /**
  2. 递归删除目录及其下的所有文件
  3. func: rrmdir
  4. */
  5. function rrmdir($dir) {
  6. if (is_dir($dir)) {
  7. $objects = scandir($dir);
  8. foreach ($objects as $object) {
  9. if ($object != “.” && $object != “..”) {
  10. if (filetype($dir.”/”.$object) == “dir”) rrmdir($dir.”/”.$object); else unlink($dir.”/”.$object);
  11. }
  12. }
  13. reset($objects);
  14. }
  15. }
  16. ?>

附: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. 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. rmdir($dir);
  11. }
  12. }
  13. ?>

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

A patch to previous script to make sure rights for deletion is set:

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

更多内容,请参考 http://cn.php.com/rmdir 。

>>>

人气教程排行