PHP Ftp_Put Fails

PHP ftp_put fails

Most typical cause of problems with ftp_put (or any other transfer command like ftp_get, ftp_nlist, ftp_rawlist, ftp_mlsd) is that PHP defaults to the active mode. And in 99% cases, one has to switch to the passive mode, to make the transfer working. Use the ftp_pasv function.

$connect = ftp_connect($ftp) or die("Unable to connect to host");
ftp_login($connect, $username, $pwd) or die("Authorization failed");
// turn passive mode on
ftp_pasv($connect, true) or die("Unable switch to passive mode");

The ftp_pasv must be called after ftp_login. Calling it before has no effect.

See also:

  • PHP ftp_put fails with "Warning: ftp_put (): PORT command successful"
  • my article on the active and passive FTP connection modes.

ftp_put returned TRUE but no file uploaded

The second argument of ftp_put is a path to a remote file, not a path to a local directory.

So it should be like:

ftp_put($conn_id, "/remote/path/somename.zip", $_FILES['files']['tmp_name'], FTP_BINARY);

Also, you should use a passive mode. See PHP ftp_put fails.

PHP's ftp_put not reporting error to screen

Use error_get_last() to print the last error.

Is there a way to get the reason when ftp_put fails?

PHP FTP functions issue a warning with the last error message returned by the server.

So when this happens:

> STOR /nonexistingfolder/readme.txt  
< 550 Filename invalid

You get a warning like:

Warning: ftp_put(): Filename invalid in test.php on line 12

There's no more information provided. Particularly you cannot enable any kind of FTP session logging.


When there's a local problem (like a non-existing local file or permissions issue), you get a warning, as with any other PHP function:

Warning: ftp_put(nonexisting.txt): failed to open stream: No such file or directory in test.php on line 12

php ftp_put() not working

Tried with different solution, that perfectly works for me -

            $ch = curl_init();
$localfile = 'abc.txt';
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, 'ftp://myserver.com/abc.txt');
curl_setopt($ch, CURLOPT_USERPWD, 'myorders:=pwd');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_PORT, 34261);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
$error_no = curl_errno($ch);

curl_close ($ch);

if ($error_no == 0) {
$error = 'File uploaded succesfully.';
} else {
$error = 'File upload error.';
}
echo $error;

PHP: Using ftp_put results in an error, how find reason?

As far as I know, the ftp_put accept 2nd parameter which is $remote_location in your example as a file name not a directory/folder name. So this variable

$remote_location = "/public_html/testdir";

is invalid assume that "/public_html/testdir" is a directory.

Hence you should change it to point to a file: "/public_html/testdir/example.csv"

Regards



Related Topics



Leave a reply



Submit