类以递归方式列出/删除目录中的文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了类以递归方式列出/删除目录中的文件相关的知识,希望对你有一定的参考价值。

Class to list/remove recursively files into a directory
  1. <?php
  2.  
  3. /*
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17.  
  18.  
  19. /*
  20. Criado por Gabriel Francisco frc.gabriel[at]gmail.com
  21.  
  22. $dir_path = caminho do diretorio que contem arquivos temporarios, cache ou qualquer coisa a ser removido
  23. */
  24. class LimpaCache {
  25.  
  26. private $dir_path;
  27.  
  28. public function setDir($dir) {
  29. $this->dir_path = $dir;
  30. }
  31.  
  32. public function listar() {
  33. if ($handle = opendir($this->dir_path)) {
  34. while (false !== ($entry = readdir($handle))) {
  35. echo "$entry </br>";
  36. }
  37. closedir($handle);
  38. }
  39. }
  40.  
  41. public function limpar() {
  42. if ($handle = opendir($this->dir_path)) {
  43. while (false !== ($entry = readdir($handle))) {
  44. if (!is_dir($entry))
  45. unlink($this->dir_path . '/' . $entry);
  46. }
  47. closedir($handle);
  48. }
  49. }
  50. }
  51.  
  52. //$limpa = new LimpaCache;
  53. //$limpa->setDir('/tmp/teste');
  54. //$limpa->listar();
  55. //$limpa->limpar();
  56. ?>

以上是关于类以递归方式列出/删除目录中的文件的主要内容,如果未能解决你的问题,请参考以下文章

以编程方式递归列出eclipse工作区中的所有文件

在Unix中递归列出文件中的所有文件? [复制]

Java.nio:最简洁的递归目录删除

递归法列出目录中的所有文件

如何在 Linux 上递归列出 C 中的目录?

Linux 目录操作