Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 444
Aditya P Bhatt Avatar asked May 27 '11 04:05

Aditya P Bhatt


3 Answers

http://php.net/ftp_pasv

$resource = ftp_connect('ftp.example.com');
ftp_login($resource, 'username', 'password');

# set this to true
ftp_pasv($resource, true);

ftp_get(...);
ftp_put(...);

I was recieving same (not very descriptive) error message E_WARNING ftp_get(): Type set to I..

I found out that it is because server running PHP did not have visible public IP (it is virtual server on my workstation).

Solution was using passive mode. Default setting (active mode) did not have problem on live server, because live server has visible public IP.

like image 182
Martin Avatar answered Nov 19 '22 08:11

Martin


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.

like image 37
Tanoro Avatar answered Nov 19 '22 08:11

Tanoro


I've tried using the ftp functions in PHP and found it was much easier to use file_put_contents() like the following:

$remote_file = "ftp://username:[email protected]/path/to/file.txt";
file_put_contents($remote_file, $file_contents);

You can still check if it was successful and all that good stuff of course too.

like image 3
dtbarne Avatar answered Nov 19 '22 07:11

dtbarne