【问题标题】:rename all files and folder ftp in php language用php语言重命名所有文件和文件夹ftp
【发布时间】:2018-05-19 13:10:57
【问题描述】:

我想以递归方式重命名所有文件和文件夹 所以当我首先重命名父文件夹时,在下一个循环中,当函数想要重命名子时说“没有这样的文件或目录”它的真实错误导致父文件夹在几分钟前重命名

我更改了我的代码但没有帮助我 从 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;

    }

这个返回数组是这样的 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;
    }
}

【问题讨论】:

  • 您忘记添加尝试通过 ftp 进行重命名的代码。
  • 如果先添加子项,如何先重命名父项?为什么还要重命名文件?给我们看重命名代码!
  • 亲爱的朋友,这是我读取文件和文件夹的代码

标签: php ftp rename file-rename


【解决方案1】:

如果你有:

+ 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;

【讨论】:

    猜你喜欢
    • 2014-09-09
    • 2012-08-10
    • 2013-04-24
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    相关资源
    最近更新 更多