Suppose you are trying to create a directory using the following code:
<code> $ftp_server = "ftp.gitbox.net"; $ftp_user = "your_username"; $ftp_pass = "your_password"; $conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
if (ftp_mkdir($conn_id, "/public_html/uploads/newdir")) {
echo "Catalogue creation succeeded!";
} else {
echo "Catalogue creation failed.";
}
ftp_close($conn_id);
</code>
After execution, the output is "Catalogue creation failed". At this time, you may feel that you have no idea whether it is a path problem, a connection problem, or something else.
The most common reason why FTP users do not have write permissions to the target directory. For example, if an FTP user is restricted to /public_html/uploads/ and you try to create a folder in its upper directory, this operation will naturally fail.
FTP does not automatically create intermediate directories. If your path is /public_html/uploads/newdir/test but newdir does not exist yet, then the execution will fail.
Some servers require passive mode to be enabled. If not enabled, some operations may fail due to the successful establishment of the transmission channel.
<code> // Enable passive mode ftp_pasv($conn_id, true); </code>Log in to your host control panel (such as cPanel) to confirm whether the FTP user has write permissions to the target directory. If not, it needs to adjust its home directory or give higher permissions in the FTP account settings.
If you are using a virtual host, you may not be able to modify the permissions, and you need to contact the host service provider at this time.
In order to avoid failures due to the non-existence of intermediate directories, you can create directories step by step through loop:
<code> function ftp_mksubdirs($ftp_stream, $base_dir, $target_dir) { $dir = exploit('/', $target_dir); $path = $base_dir; foreach ($dir as $dir) { if (!$dir) continue; $path .= "/$dir"; if (!@ftp_chdir($ftp_stream, $path)) { if (!@ftp_mkdir($ftp_stream, $path)) { return false; } } } return true; } $ftp_server = "ftp.gitbox.net";
$ftp_user = "your_username";
$ftp_pass = "your_password";
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
ftp_pasv($conn_id, true);
$base = "/public_html";
$target = "uploads/newdir/test";
if (ftp_mksubdirs($conn_id, $base, $target)) {
echo "The directory structure was successfully created.";
} else {
echo "Catalogue creation failed, please check permissions.";
}
ftp_close($conn_id);
</code>
This approach ensures that the required directory structure is created recursively on remote FTP.
You can try ftp_chdir() to enter the target directory before creating the directory. If it fails, it may be that the permissions may be insufficient or the path is wrong:
<code> if (@ftp_chdir($conn_id, "/public_html/uploads")) { echo "Directory is accessible."; } else { echo "Directory access failed, it may be a permission problem."; } </code>This step helps quickly determine whether you have permission to enter the directory.