首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用php语言重命名所有文件和文件夹ftp

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

Stack Overflow用户
提问于 2017-12-06 03:12:23
回答 0查看 333关注 0票数 1

我想要递归地重命名所有的文件和文件夹我写了一个函数,它可以用来重命名文件,但我的问题是当我在路径中创建文件和文件夹的数组时,它不能作为父子文件夹排序,所以当我首先重命名父文件夹时,在下一次循环中,当函数想要重命名子文件夹时,它会说“没有这样的文件或目录”它的真正错误导致几分钟前父文件夹被重命名

我更改了我的代码,但没有帮助我从ftp读取文件和文件夹的代码:

代码语言:javascript
复制
 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

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

代码语言:javascript
复制
$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

代码语言:javascript
复制
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;
    }
}
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47660990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档