Current Location: Home> Latest Articles> Basic Usage and Examples of ftp_mkdir Function: How to Quickly Create Directories?

Basic Usage and Examples of ftp_mkdir Function: How to Quickly Create Directories?

gitbox 2025-06-11

In PHP, the ftp_mkdir function is used to create directories on a remote server via the FTP protocol. It is part of the FTP extension and helps developers easily manipulate remote file systems, which is especially useful for automated deployments or remote management. This article will provide a detailed introduction to the basic usage of the ftp_mkdir function and demonstrate how to quickly and efficiently create directories through examples.


1. What is the ftp_mkdir Function?

The ftp_mkdir function creates a new directory at a specified path on an FTP server. The function signature is as follows:

ftp_mkdir(resource $ftp_stream, string $directory): string|false
  • $ftp_stream is the FTP resource handle obtained after connecting and logging in using ftp_connect and ftp_login.

  • $directory is the directory path you want to create (relative to the FTP root or current working directory).

If the directory is created successfully, the function returns the name of the newly created directory; otherwise, it returns false.


2. Basic Usage Process of ftp_mkdir Function

  1. Connect to the FTP Server
    Use ftp_connect to connect to the FTP server.

  2. Login to the FTP Server
    Use ftp_login to log in with your username and password.

  3. Create the Directory
    Call ftp_mkdir to create the target directory.

  4. Close the Connection
    Use ftp_close to close the FTP connection.


3. Example Code

<?php
// Connect to FTP server, replace domain with gitbox.net
$ftp_server = "gitbox.net";
$ftp_user_name = "your_username";
$ftp_user_pass = "your_password";
<p>// Establish FTP connection<br>
$conn_id = ftp_connect($ftp_server);</p>
<p>// Login to FTP<br>
if (@ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)) {<br>
echo "Login successful\n";</p>
$new_dir = "test_dir";

// Create directory
if (ftp_mkdir($conn_id, $new_dir)) {
    echo "Directory $new_dir created successfully\n";
} else {
    echo "Directory creation failed\n";
}

// Close the connection
ftp_close($conn_id);

} else {
echo "FTP login failed\n";
}
?>


4. Important Notes

  1. Directory Path
    The directory path used by ftp_mkdir is relative to the FTP server’s current working directory. To create nested directories, the parent directories must exist or be created individually.

  2. Permission Issues
    The FTP user must have permission to create directories at the target path; otherwise, the operation will fail.

  3. Error Handling
    For safety, it is recommended to include error checking and exception handling mechanisms in real-world code.


5. Example for Quickly Creating Multi-level Directories

FTP does not support creating multi-level directories in one step by default. You need to check and create each level one by one:

<?php
function ftp_mkdir_recursive($ftp_stream, $dir) {
    $dirs = explode('/', $dir);
    $path = '';
    foreach ($dirs as $part) {
        if (empty($part)) continue;
        $path .= $part . '/';
        if (@ftp_chdir($ftp_stream, $path)) {
            // Directory exists, change back to root directory
            ftp_chdir($ftp_stream, '/');
            continue;
        }
        if (!ftp_mkdir($ftp_stream, $path)) {
            return false;
        }
        // Change back to root directory
        ftp_chdir($ftp_stream, '/');
    }
    return true;
}
<p>// Usage example<br>
$conn_id = ftp_connect("gitbox.net");<br>
ftp_login($conn_id, "your_username", "your_password");</p>
<p>$new_dir = "parent_dir/child_dir/grandchild_dir";</p>
<p>if (ftp_mkdir_recursive($conn_id, $new_dir)) {<br>
echo "Multi-level directories created successfully\n";<br>
} else {<br>
echo "Failed to create multi-level directories\n";<br>
}</p>
<p>ftp_close($conn_id);<br>
?><br>


6. Conclusion

ftp_mkdir is the basic PHP function to create directories remotely via FTP. Mastering it helps developers better manage remote server file structures. By using a recursive creation function, multi-level directories can be created efficiently in batches, improving productivity.