如何用PHP实现删除整个目录及其所有文件的长尾操作?
- 内容介绍
- 文章标签
- 相关推荐
本文共计118个文字,预计阅读时间需要1分钟。
我们可以使用`RecursiveDirectoryIterator`和`RecursiveIteratorIterator`来删除目录及其子目录和文件。以下是一个简化版的代码开头,不超过100个字:
phpisFile()) { unlink($file->getPathname()); } elseif ($file->isDir()) { rmdir($file->getPathname()); } }}
我们可以使用RecursiveDirectoryIterator 和 RecursiveIteratorIterator删除目录和子目录及文件,子目录将先与父目录删除
<?php function cleanup_directory($dir) { $iter = new RecursiveDirectoryIterator($dir); foreach (new RecursiveIteratorIterator($iter, RecursiveIteratorIterator::CHILD_FIRST) as $f) { if ($f->isDir()) { rmdir($f->getPathname()); } else { unlink($f->getPathname()); } } rmdir($dir); } cleanup_directory('c:\\wamp\\junk'); ?>
本文共计118个文字,预计阅读时间需要1分钟。
我们可以使用`RecursiveDirectoryIterator`和`RecursiveIteratorIterator`来删除目录及其子目录和文件。以下是一个简化版的代码开头,不超过100个字:
phpisFile()) { unlink($file->getPathname()); } elseif ($file->isDir()) { rmdir($file->getPathname()); } }}
我们可以使用RecursiveDirectoryIterator 和 RecursiveIteratorIterator删除目录和子目录及文件,子目录将先与父目录删除
<?php function cleanup_directory($dir) { $iter = new RecursiveDirectoryIterator($dir); foreach (new RecursiveIteratorIterator($iter, RecursiveIteratorIterator::CHILD_FIRST) as $f) { if ($f->isDir()) { rmdir($f->getPathname()); } else { unlink($f->getPathname()); } } rmdir($dir); } cleanup_directory('c:\\wamp\\junk'); ?>

