【发布时间】:2018-05-19 13:10:57
【问题描述】:
我想以递归方式重命名所有文件和文件夹 所以当我首先重命名父文件夹时,在下一个循环中,当函数想要重命名子时说“没有这样的文件或目录”它的真实错误导致父文件夹在几分钟前重命名
我更改了我的代码但没有帮助我 从 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;
}
}
【问题讨论】:
-
您忘记添加尝试通过 ftp 进行重命名的代码。
-
如果先添加子项,如何先重命名父项?为什么还要重命名文件?给我们看重命名代码!
-
亲爱的朋友,这是我读取文件和文件夹的代码
标签: php ftp rename file-rename