用 php 语言重命名所有文件和文件夹 ftp

rename all files and folder ftp in php language

我想递归地重命名所有文件和文件夹我编写了一个可以工作并重命名文件的函数但是当我在路径中创建文件和文件夹数组时出现问题,它不排序为父子 所以当我首先重命名父文件夹时,在下一个循环中当函数想要重命名子文件夹时说 "No such file or directory" 它是真的错误导致几分钟前重命名父文件夹

我更改了我的代码但没有帮助我 从 ftp :

读取文件和文件夹的代码
 if (!self::ftp_is_dir($resource, $thisPath)) {
        // for Files (anything that isnt a readable directory)
        if ($first == TRUE) {
            return array("Path doesn't Exist (" . $thisPath . ")");
        }
        $theList[] = $thisPath;
        return $theList;
    } else {
        $contents = ftp_nlist($resource, $thisPath);

        // For empty folders
        if (count($contents) == 0) {
            $theList[] = $thisPath;
            return $theList;
        } else {
            $theList[] = $thisPath;
        }

        // Recursive Part
        foreach ($contents As $file) {
            $theList = self::ftp_nlistr($resource, $file, $theList, FALSE);
        }

        return $theList;

    }

和这个 return 这样的数组 enter image description here

我用来重命名文件夹和文件的代码

$replacers = array(" ", "", "  ", "-=", "=-", '©',"!", ";", "#", "@", "'", '<', '>');
    foreach ($paths as $path) {
        if (preg_match('/' . implode('|', $replacers) . '/', $path) != 0) {

            $route = preg_replace('/ftp/', "ftp://ftp.mylocal.co", $path, 1);;
            if (is_dir($route)) {
                $newName = str_replace($replacers, "_", basename($path));
                $directory = pathinfo($path);
                if (ftp_rename($connectionID, $path, $directory['dirname'] . '/' . $newName)) {
                    Logger::setLog('renaming', "Renaming: $path to $newName");
                } else {
                    Logger::setLog('failed to renaming', "Renaming: $path to $newName");
                }
            } else {
                $newName = str_replace($replacers, "_", basename($path));
                $directory = pathinfo($path);

                if (ftp_rename($connectionID, $path, $directory['dirname'] . '/' . $newName)) {
                    Logger::setLog('renaming', "Renaming: $path to $newName");
                } else {
                    Logger::setLog('failed to renaming', "Renaming: $path to $newName");
                }

            }
        }
    }

[1]: https://i.stack.imgur.com/xk3kx.png

public static function ftp_is_dir($conn, $dir)
{
    $cdir = ftp_pwd($conn);
    if (@ftp_chdir($conn, $dir)) {
        ftp_chdir($conn, $cdir);
        return true;
    } else {
        return false;
    }
}

如果你有:

+ folder
  - file1
  - file2
+ folder with space
  - file with space

...您目前首先将 /folder with space 重命名为 /folder_with_space

然后您尝试将 /folder with space/file with space 重命名为 /folder with space/file_with_space。但是那个文件已经不存在了。

最简单的解决方案实际上是首先重命名子项,然后是父项:

    $contents = ftp_nlist($resource, $thisPath);

    // Recursive Part
    foreach ($contents As $file) {
        $theList = self::ftp_nlistr($resource, $file, $theList, FALSE);
    }

    $theList[] = $thisPath;

    return $theList;