php递归迭代器在文件重命名时失败

php递归迭代器在文件重命名时失败,php,recursiveiterator,Php,Recursiveiterator,我对RecursiveIteratorIterator移动到下一项有问题。我有一个目录,其中有多个文件,我试图去每个文件,重命名它,并做一些其他的事情。递归迭代器从目录中选取第一个文件并成功重命名它。当我执行$it->next()时,它将保持在同一个文件中,并尝试查找已重命名为其他文件的文件。下面是我的代码示例。文件权限设置为777。任何想法都将不胜感激 只有重命名文件时才会发生这种情况。如果我删除重命名功能,它将按预期移动到下一项 //initialize iterator object

我对RecursiveIteratorIterator移动到下一项有问题。我有一个目录,其中有多个文件,我试图去每个文件,重命名它,并做一些其他的事情。递归迭代器从目录中选取第一个文件并成功重命名它。当我执行$it->next()时,它将保持在同一个文件中,并尝试查找已重命名为其他文件的文件。下面是我的代码示例。文件权限设置为777。任何想法都将不胜感激

只有重命名文件时才会发生这种情况。如果我删除重命名功能,它将按预期移动到下一项

   //initialize iterator object
  $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, 0));

 /*  loop directly over the object */

while ($it->valid()) {
// check if value is a directory 

if (!$it->isDot()) {
    if (!is_writable($directory . '/' . $it->getSubPathName())) {

        //direcotry not writable, throw error

    } else {

        // get current file info
        $fileinfo = pathinfo($it->getSubPathName());

        //get file extension
        $ext = $fileinfo['extension'];

        //if its a '.', move to next item
        if (in_array($fileinfo['filename'], array(".", ".."))) {
            $it->next();
        }

        // the current file name with complete path
        $old_file = $directory . '/' . $it->getSubPathName();
        throw new Exception('source file doesnot exist ' . $old_file, error::DOESNOTEXIST);
        }

        //generate new file name with path
        $new_file = $directory . '/' . $it->getSubPath() . '/' . $filename . '.' . $ext;

        //rename the file
        rename($old_file, $new_file);

    }

}
/*             * * move to the next iteration ** */
$it->next();

}

这段代码有点凌乱,有没有可能把它简化为基本代码?$old_file=$directory.'/'$it->getSubPathName();根据手册,这应该包含实际的文件名和扩展名。我已经删除了不必要的代码并格式化了一点。让我们看看plz.$it->getSubPathName()为您提供实际的文件名