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