[英]rename all files and folder ftp in php language
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 : 我更改了代码,但不帮我从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 [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;
}
}
If you have: 如果你有:
+ folder
- file1
- file2
+ folder with space
- file with space
... you currently first rename /folder with space
to /folder_with_space
. ...您当前首先将
/folder with space
重命名为/folder_with_space
。
And then you try to rename /folder with space/file with space
to /folder with space/file_with_space
. 然后,您尝试将
/folder with space/file with space
重命名为/folder with space/file with space
/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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.