我想要递归地重命名所有的文件和文件夹我写了一个函数,它可以用来重命名文件,但我的问题是当我在路径中创建文件和文件夹的数组时,它不能作为父子文件夹排序,所以当我首先重命名父文件夹时,在下一次循环中,当函数想要重命名子文件夹时,它会说“没有这样的文件或目录”它的真正错误导致几分钟前父文件夹被重命名
我更改了我的代码,但没有帮助我从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;
}
}https://stackoverflow.com/questions/47660990
复制相似问题