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

Learn More

How to rename a file/directory on ftp with ftp_rename()

74 Runs 1478 Views 220 Copies


gtsolutions 373

published 4 years ago


 * PHP ftp module
 * reanme a file/directory from ftp with ftp_rename()

// 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 rename a file/directory (the file/directory won't be renamed 
    //because is a public ftp and we do not have the rights to write there)
    $oldName = "pub";
    $newName = "public";
    if (ftp_rename($conn_id, $oldName, $newName)) {
        echo "<br /><br /><b>successfully renamed <b>$oldName</b> to <b>$newName</b>";
    } else {
        echo '<div style="background-color:red;padding:10px;color:#fff;font-size:16px">';
        echo "Could not rename <b>$oldName</b> to <b>$newName</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).