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
PHP Simplexml Namespace Problem
How Can One Run Multiple Versions of PHP 5.X on a Development Lamp Server
How to Re-Save the Entity as Another Row in Doctrine 2
PHP Can't Connect to MySQL with Error 13 (But Command Line Can)
Curl and Https, "Cannot Resolve Host"
_Construct() VS Sameasclassname() for Constructor in PHP
Ini_Set("Memory_Limit") in PHP 5.3.3 Is Not Working at All
Using Nulls in a MySQLi Prepared Statement
Warning: MySQLi_Query(): Couldn't Fetch MySQLi
PHP File Upload, How to Restrict File Upload Type
How to Convert Boolean to String
Laravel Catch Tokenmismatchexception
How to Use Preg_Replace_Callback
Codeigniter: Load Controller Within Controller
Does PHP Execution Stop After a User Leaves the Page
Why am I Getting Mime-Type of .CSV File as "Application/Octet-Stream"
How to Split a String in PHP at the Nth Occurrence of a Needle