更新且正确的递归目录制作代码
/**
* 递归创建 ftp 目录的函数。
*
* @param $ftpcon ftp 连接资源
* @param $baseDir
* @param $path
* @param int $mode
* @return bool
*/
function ftp_mksubdirs($ftpcon, $baseDir, $path, $mode = 0775) {
$path = dirname($path);
if ($baseDir !== DIRECTORY_SEPARATOR && strpos($path, $baseDir) !== false) {
if (empty($path)) {
// $baseDir 等于 $path,它只能表示此文件夹已存在
return false;
}
if ($path[0] == DIRECTORY_SEPARATOR) {
$path = substr($path, 1);
}
} else {
if ($path[0] == DIRECTORY_SEPARATOR) {
$path = substr($path, 1);
}
}
@ftp_chdir($ftpcon, $baseDir);
$parts = explode(DIRECTORY_SEPARATOR, $path);
$created = [];
foreach ($parts as $part) {
if (!@ftp_chdir($ftpcon, $part)) {
$createdDir = @ftp_mkdir($ftpcon, $part);
if ($createdDir !== false) {
ftp_chmod($ftpcon, $mode, $createdDir);
}
$created[] = $createdDir;
@ftp_chdir($ftpcon, $part);
}
}
return !in_array(false, $created);
}