ftp_put() very slow with little file - php

I've made a form that is waiting for a file. Once it's submitted, the file is uploaded to a remote server through the ftp_put() function. The problem is that it take something like 2/3 minuts.
I tried removing lines one by one to find out what's wrong and it's the call to ftp_put() that is taking so much time. The file is only 20Ko. Is there a way to upload the file in an other thread or something ?
EDIT :
Here is my php code.
$file = $_FILES['attachment']['tmp_name'];
$ext = pathinfo($_FILES['attachment']['name'], PATHINFO_EXTENSION);
$remote_file = '/cv_'.$_POST['first_name'].'_'.$_POST['last_name'].'_'.strval(time()).'.'.$ext;
$ftp_server = 'xxxxxxxxx';
$ftp_user_name = 'xxxxxx';
$ftp_user_pass = 'xxxxxx';
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (!ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) /* This line is taking so long */
{
echo "There was a problem uploading the file $file\n";
}
ftp_close($conn_id);
EDIT 2 : The file is correctly uploaded after a few minuts. The code is working fine and fast on localhost, but when it's on the production server, it starts taking a long time.

Solved it enabling passive mode.

Related

Upload files with PHP on FTP server (WinSCP works)

I want to upload a file on a FTP Server (I use WinSCP). But I don't know how use ftp_put.
$file = 'somefile.txt';
$remote_file = 'readme.txt';
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
ftp_close($conn_id);
I receive the file with a form ($_FILES). In $file, maybe $_FILES['myFile']['name'] but I don't know what to put in $ftp_server and $remote_file. The server http://10.XX.X.XX/myProject/ and my file, in WinSCP are in : /var/www/myProject/
Maybe the hostname is XXXX#xxxxx
It works ! Thanks. The problem was the path, the good is : /myProject/
$ftp_server should be the host name you use, when connecting with WinSCP, like "example.com".
$remote_file should be "/var/www/myProject/" . $_FILES['myFile']['name'].
$file should be $_FILES["myFile"]["tmp_name"].

Upload error for PHP file - 500 internal server error?

I have upload script for file uploading from php which is configured to upload file on hosting server from godaddy. Same script I am using on different server domain it is not able to upload on that server which is also godaddy server. I have full access to the folder and server ftp but still didnt find out why this script is not working on that server. Anyone can find out why it is throwing 500 error.
<?php
$ftp_server = "ftp.xxxxx.com";
$ftp_username ="xxxx#xxxx.com";
$ftp_password = "xxxxxxxxxxx";
$destination_folder = "/upload/";
$file_name = $_FILES["userfile"]["name"];
$destination_file = $destination_folder.time().'_'.$file_name;
$file = $_FILES["userfile"]["tmp_name"];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);
// upload a file
if (ftp_put($conn_id, $destination_file, $file, FTP_BINARY)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
Do you have shell access? If so, reconfig apache. It may sound not related to your problem, but this will solve it. I had this problem some time ago and rebuilding apache fixed it.

Moving a file to another server via FTP using PHP

I need to move a backup of my database file to another server automatically using cron. I have some code and it's connecting to the ftp server fine, the problem is it will not move the file. I don't know how to make it show the exact error other than "FTP upload failed!" so I don't know what the real problem is, however, I believe it's got to do with the "real" path of the file on the server and/or the real path of the directory of the other server where it's placing the file.
Any help would be great.
$server = "MyServer";
$ftp_user_name = "MyUsername";
$ftp_user_pass = "MyPassword";
$source = "bumail_data/db-backup-05-09-15__17:23.sql";
$dest = 'db-backup-05-09-15__17:23.sql';
$connection = ftp_connect($server);
$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);
ftp_pasv ( $connection, true );
if (!$connection || !$login) { die('Connection attempt failed!'); }
$upload = ftp_put($connection, $dest, $source, FTP_BINARY);
if (!$upload) { echo 'FTP upload failed!'; }
ftp_close($connection);
The file $source is located in httpdocs/bumail_data/FILE.sql
The folder on the other server I want to put the file in is located before the public_html on a dir named dbbackup. So I guess that is the root dir but not sure.
I hope that helps you understand where the files are and what I maybe need to put in the script. I have tried everything I can think of from ../dir/file to var/host/root/dir/file and so on. It just will not move the file.

Send files from One server to another.

I have large files (total ~250mb) on my web server. I want to send these files quickly to my 2nd server without using an ftp client. I know the ftp password, username etc of my 2nd server, but how to do it without an ftp client?
$file = 'somehugefile.big';
$remote_file = '/uploads/somehugefile.big';
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
This example is adapted from the official docs -> http://www.php.net/manual/en/function.ftp-put.php
You will have to change 'upload_max_filesize' and 'post_max_size' in PHP.ini to a higher value as well.
ini_set('upload_max_filesize', '250M');
ini_set('max_execution_time', '999');
ini_set('memory_limit', '500M');
ini_set('post_max_size', '250M');

FTP file upload using php

I need to upload a file automatically from a local computer to a remote server. I have found the following code on here:
<?php
require_once('ftp.php');
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
// close the connection
ftp_close($conn_id);
?>
ftp.php is my file with the ftp authentication information. The connection works but I am getting the following error:
There was a problem while uploading C:/xampp/htdocs/testbcos/accounting/checkslastmonth.csv
EDIT: I amnot sure if this makes a difference or not, but here are my $remote_file and my $file:
$file = "C:/xampp/htdocs/testbcos/accounting/checkslastmonth.csv";//tobe uploaded
$remote_file = "/home/bookcell/public_html/testbcos/accounting/checkslastmonth3.csv";
What am I doing wrong here? Also, is it possible to do this if the file is on a mapped drive on my local server?
Thanks.
First thing: Try to set passive mode. You need it if you're sitting behind a firewall. (What probably is the case)
ftp_pasv($conn_id, true); // after ftp_login
Second, you have to change to dir first:
ftp_chdir($conn_id, '/home/bookcell/public_html/testbcos/accounting/');
ftp_put($conn_id, 'checkslastmonth3.csv', $file, FTP_ASCII);
If you want to know what's really going on, try to get error message with error_get_last() or
$php_errormsg

Categories