php递归遍历删除文件的方法

这篇文章主要介绍了php递归遍历删除文件的方法,涉及php遍历文件操作的相关技巧,非常具有实用价值,需要的朋友可以参考下。

本文实例讲述了php递归遍历删除文件的方法,分享给大家供大家参考,具体如下:

这个函数稍加修改就可以变成一个递归文件拷贝函数:

  1. <?php
  2. function mover($src,$dst) {
  3. $handle=opendir($src);
  4. // Opens source dir.
  5. if (!is_dir($dst)) mkdir($dst,0755);
  6. // Make dest dir.
  7. while ($file = readdir($handle)) {
  8. if (($file!=".") and ($file!="..")) {
  9. // Skips . and .. dirs
  10. $srcm=$src."/".$file;
  11. $dstm=$dst."/".$file;
  12. if (is_dir($srcm)) {
  13. // If another dir is found
  14. mover($srcm,$dstm);
  15. // calls itself - recursive WTG
  16. } else {
  17. copy($srcm,$dstm);
  18. unlink($srcm);
  19. // Is just a copy procedure is needed
  20. } // comment out this line
  21. }
  22. }
  23. closedir($handle);
  24. rmdir($src);
  25. }
  26. ?>