Speed up development with full-stack environments for every branch.

Learn More

How to create a directory on a ftp with ftp_mkdir()

126 Runs 2055 Views 591 Copies


gtsolutions 373

published 4 years ago


 * PHP ftp module
 * create a directory from ftp connection with ftp_mkdir()

// set up the url for the connection
$ftp_server = "ftp.bls.gov";
//connect to the server
$conn_id = ftp_connect($ftp_server);

// login with username and password (if you want to use anonymous connection 
//use the username: anonymous and leave the password field empty)
$ftp_user_name = "anonymous";
$ftp_user_pass = "";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// check connection
echo "<center>";
if ((!$conn_id) || (!$login_result)) {
    echo '<div style="background-color:red;padding:10px;color:#fff;font-size:16px;">';
        echo "FTP connection has failed!";
        echo "Attempted to connect to <b>$ftp_server</b> for user <b>$ftp_user_name</b>";
    echo '</div>';
} else {
    echo '<div style="background-color:green;padding:10px;color:#fff;font-size:16px">';
        echo "Connected to <b>$ftp_server</b>, for user <b>$ftp_user_name</b>";
    echo '</div>';
    // try to create a directory (the directory won't be created because 
    //is a public ftp and we do not have the rights to write there)
    $directory = "pub/test";
    if (ftp_mkdir($conn_id, $directory)) {
        echo "<br /><br /><b>$directory</b> created";
    } else {
        echo '<div style="background-color:red;padding:10px;color:#fff;font-size:16px">';
            echo "Could not create directory: <b>$directory</b>";
        echo '</div>';
echo "</center>";

// close the FTP stream 
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).