I am trying to upload a csv file to my ftp server -
$file = 'abc.csv';
$remote_file = 'orders/abc.csv';
$ftp_server = "myserver.com";
$conn_id = ftp_connect($ftp_server, 34261);
ftp_pasv($conn_id, false);
$login_result = ftp_login($conn_id, "myorders", 'pwd');
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file
";
} else {
echo "There was a problem while uploading $file
";
}
ftp_close($conn_id);
But, file doesn't get transferred. Destination folder has read write permissions. Also, I have checked with FTP_ASCII
and FTP_BINARY
methods. abc.txt is in my project's root folder. But I cannot able to track the exact error. How should I debug the code?
Any help is appreciated.
Thanks.
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;
You should increase the error reporting level of PHP, so that you can see warnings and notices.
Be sure that you can do the same transfer from the same machine, with a regular ftp client.