2017-12-05 14 views
1

すべてのファイルとフォルダの名前を再帰的に変更したいファイルと名前を変更する関数を作成しましたが、親の子 親フォルダの名前を最初に変更するときに、次のループで関数の名前を変更するときに「No such file or directory」と表示されます。 ftpからファイルとフォルダを読み取るコード:すべてのファイルとフォルダの名前をPHP言語で変更します

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

あなたはFTPを介して名前変更を行い未遂のコードを追加するのを忘れ:

最も簡単な解決策は、実際にはまず、親、子供の名前を変更することです。 –

+1

子を最初に追加した場合、どのように親の名前を最初に変更しますか?なぜあなたはファイルの名前を変更しますか?名前を変更してください! –

+0

こんにちは親愛なる友人これはファイルとフォルダを読むための私のコードです –

答えて

0

の名前を変更するために使用されるこのコードのように、このリターン・アレー:

+ 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; 
関連する問題