PHP递归文件&文件夹删除功能:“目录不为空”错误信息
我正在尝试创建一个函数,该函数删除虚拟主机上的所有文件和目录,不包括给定的文件和文件夹数组
function cleanUp($exdirs, $exfiles){
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator('.'),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach($it as $entry) {
if ($entry->isDir() && !in_array($entry->getBasename(), $exdirs)) {
rmdir($entry->getPathname());
}
else {
if (!in_array($entry->getFileName(), $exfiles)) {
unlink($entry->getPathname());
}
else {
$exdirs[] = dirname($entry->getFileName());
}
}
}
}
并像这样调用此函数
$excludeDirsNames = array('cgi-bin');
$excludeFileNames = array('.htaccess', 'ws.zip', 'update.php');
cleanUp($excludeDirsNames , $excludeFileNames);
现在的问题是,它删除但收到错误消息:Directory not empty on line rmdir($ Entry->getPathname());
多次。如何解决这个问题?
I'm trying to create function which removes all file and directories on webhosting excluding given files and folders arrays
function cleanUp($exdirs, $exfiles){
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator('.'),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach($it as $entry) {
if ($entry->isDir() && !in_array($entry->getBasename(), $exdirs)) {
rmdir($entry->getPathname());
}
else {
if (!in_array($entry->getFileName(), $exfiles)) {
unlink($entry->getPathname());
}
else {
$exdirs[] = dirname($entry->getFileName());
}
}
}
}
And calling this function like this
$excludeDirsNames = array('cgi-bin');
$excludeFileNames = array('.htaccess', 'ws.zip', 'update.php');
cleanUp($excludeDirsNames , $excludeFileNames);
Now the problem is, it deletes but getting error message: Directory not empty on line rmdir($entry->getPathname());
several times. How to fix that problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您允许排除文件和目录,但不测试目录是否包含之前排除的其他文件或目录。
只是一个简单的前缀比较:“dir”是排除路径之一的前缀吗?仅适用于绝对路径(以及其他一些小事情),但它应该解释这是怎么回事。
You allow to exclude files and directories, but you don't test, if a directory contains other files, or directories, that were excluded before.
Just a simple prefix comparison: Is "dir" is prefix of one of the excluded paths? Only works for absolute paths (and some other minor things), but it should explain, whats the matter.
以下是它不起作用的两个原因:
1)在您的子文件夹之一中有您排除的文件。由于没有删除,因此无法“rm”该文件夹。
2) 在“unlink”和“rmdir”-Funktion 调用“clearstatcache();”之后。我想这会解决你的问题。文件已删除,但信息在缓存中仍然可用。
Here are two reasons why it not work:
1) In one of your child-folder are files you are excluding. There are not deleted, so it is not possible to "rm" the folder.
2) After the "unlink" and "rmdir"-Funktion call "clearstatcache();". I think this will solve your problem. The files are delete, but the information is still available in the cache.