Search code examples
phpfileftptransfer

PHP ftp_put warning Warning: ftp_put() [function.ftp-put]: Type set to I. in


When i try to upload files using PHP's ftp_put function, earlier it was erroring:

Warning: ftp_put() [function.ftp-put]: No data connection

Now, i tried to put passive mode on:

ftp_pasv($conn_id, true);

then comes error:

Warning: ftp_put() [function.ftp-put]: Type set to I. in

ftp_login is done properly and it says Successfully.

Now it gives new warning: Warning: ftp_put() [function.ftp-put]: abc.txt: Cannot open or remove a file containing a running program.

Any ideas, why file not tranferring ?

Thanks !

Here is my code snippet:

    $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");

    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("You do not have access to this ftp server!");

    if ((!$conn_id) || (!$login_result)) {
        // wont ever hit this, b/c of the die call on ftp_login
        echo "<span style='color:#FF0000'><h2>FTP connection has failed! <br />";
        echo "Attempted to connect to $ftp_server for user $ftp_user_name</h2></span>";
        exit;
    } else {
        //echo "Connected to $ftp_server, for user $ftp_user_name <br />";
    }

    //turn passive mode on
    ftp_pasv($conn_id, true);

    $upload = ftp_put($conn_id, $destination_file.$name, $filename, FTP_BINARY);

    if (!$upload) {
        echo "<span style='color:#FF0000'><h2>FTP upload of $filename has failed!</h2></span> <br />";
    } else {
        echo 'Uploaded';    
    }

 ftp_close($conn_id);

Solution

  • The last error you are seeing happens when the FTP daemon is stuck with the uploaded file open and waiting for you to write to it.

    Anytime you successfully open a connection over an FTP server, be prepared to close the connection with the following function when the process completes or terminates due to any errors.

    ftp_close($conn_id);
    

    It's possible your script is leaving its connections open and the FTP server is getting confused by this. Try adding ftp_close in the appropriate places and see if the script runs more smoothly.