Updated and correct code for making recursive directories:
/**
* A function to create ftp directories recursively.
*
* @param $ftpcon the ftp connection resource
* @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 equals to $path, it can only mean that this folder already exists
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);
}