Current Location: Home> Latest Articles> Solve the permission issue of ftp_mkdir failed to create a directory

Solve the permission issue of ftp_mkdir failed to create a directory

gitbox 2025-05-31

Problem description

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.


Common Cause Analysis

1. Insufficient permissions

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.

2. The target path does not have an intermediate directory

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.

3. Passive mode and connection method issues

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>

Solution

Method 1: Check and modify FTP user permissions

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.


Method 2: Create a directory step by step

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.


Method 3: Use ftp_chdir for permission testing

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.