0

我在将 RecursiveIteratorIterator 移至下一项时遇到问题。我有一个包含多个文件的目录,我正在尝试访问每个文件,重命名它,然后在上面做一些其他的事情。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();

}

4

0 回答 0