dryift6733 2017-12-05 11:12
浏览 106
已采纳

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

i want rename all files and folder recursively i written a function that works and rename files but my problem when occurred when i make array of files and folders in a path, its not sort as parent child so when i rename parent folder first , in next loop when function wants rename child said "No such file or directory" its true Error cause parent folder renamed a couple minutes ago

i changed my code but not help me code for reading files and folder from 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;

    }

and this return array like this enter image description here

and this code i used for renaming folder and files

$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;
    }
}

展开全部

  • 写回答

1条回答 默认 最新

  • duanmu5039 2017-12-06 06:25
    关注

    If you have:

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

    ... you currently first rename /folder with space to /folder_with_space.

    And then you try to rename /folder with space/file with space to /folder with space/file_with_space. But that file does not exist anymore.

    The easiest solution is to actually first rename children, then parent:

        $contents = ftp_nlist($resource, $thisPath);
    
        // Recursive Part
        foreach ($contents As $file) {
            $theList = self::ftp_nlistr($resource, $file, $theList, FALSE);
        }
    
        $theList[] = $thisPath;
    
        return $theList;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
  • ¥500 python 的API,有酬谢
  • ¥15 软件冲突问题,软件残留问题
  • ¥30 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥50 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!