How to upload large file(s) on FTP server using php? - php

$ftp_server = 'ftp.abc.com';
$remote_file = "myvideo.avi"; // file size 210MB
$file = "myvideo.avi"; // file size 210MB
$conn_id = ftp_connect($ftp_server); // set up basic connection
// login with username and password
$login_result = ftp_login($conn_id, 'faraz#abc.com', 'password');
ftp_pasv($conn_id, true); // turn passive mode on
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
i already edit php.in for
upload_max_filesize = 1024M
above snippet working on all types of file(s) upload,
but when am trying to upload a large size file then it upload on server but size varies after uploading.
A video file size-210MB on local-machine
but after upload, it displayed file size 328KB on FTP-server.
i have no idea where do i check, if someone has suggestion then i appreciates

Use FTP_BINARY instead of FTP_ASCII. The latter might stop at the first NUL byte.
Besides that, there is no good reason to use ascii mode nowadays, even for ascii files. The only case where it's important if you have cgi scripts containing a shebang line that have windows linebreaks locally.

Related

Uploading file with ftp_nb_put to FTP server in PHP results in an empty destination file

Hello,
I'm doing a script to send files directly to a server by ftp protocol.
To do that, I use php ftp functions.
$conn_id = ftp_connect('XXXX.com', 21);
$login_result = ftp_login($conn_id, 'USER_XXXX', 'PWD_XXXX');
ftp_pasv($conn_id, true) or die("Cannot switch to passive mode");
if ((!$conn_id) || (!$login_result)) {
die("Problem FTP connection !");
}
if (ftp_chdir($conn_id, 'SERVER_PATH')) {
echo ftp_pwd($conn_id) . "\n";
} else {
echo "Problem to change path\n";
}
ftp_nb_put($conn_id, $File, $localPath. $File, FTP_ASCII);
ftp_close($conn_id);
No error is announced, but the file that arrives on my server is empty.
I forgot to precise, my file to upload is a big xlsx file.
Your code is just wrong. You use non-blocking ftp_nb_put, as if it were blocking.
If you want a simple code, you have to use ftp_put.
If you really need a non-blocking code, you have to close the connection only after ftp_nb_continue reports FTP_FINISHED. See File uploaded with ftp_nb_put to FileZilla FTP server in PHP is corrupted.
Otherwise your current code will just close the connection at a moment the upload hardly even started.
The "sleep", suggested in comments, is (as mentioned there) just for a test, you cannot reliably use it in a real code.
Your second problem is that you are uploading binary .xlsx files in a text/ascii mode (FTP_ASCII). You have to use a binary mode (FTP_BINARY). The text/ascii mode will corrupt binary files. So even, if the upload finishes, the file will be corrupted.
This will work:
ftp_put($conn_id, $File, $localPath.$File, FTP_BINARY);

PHP FTP Not Working Using Passive Mode

I have a script that was working before, but we recently changed the FTP location and is no longer working correctly. I have tried setting to passive mode and switching between ascii / binary with no luck.
Below is my script, is there something obvious I'm missing? Is there a way to easily identify the error? The specified directory has all permissions. It connects just fine and I can list the directory contents and see all the folders correctly...just doesn't upload.
$file = $createdAt.".txt";
$fp = fopen('exportorder/'.$file, 'r');
$conn_id = ftp_connect('ftp.location.com') or die("Couldn't connect to ftp.location.com");;
$login_result = ftp_login($conn_id, 'username', 'pass');
ftp_pasv($conn_id,TRUE);
ftp_chdir($conn_id, '/OTD-0000117');
if (ftp_fput($conn_id, 'exportorder/'.$file, $fp, FTP_BINARY)) {
echo "Successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
Thanks in advance!
Use ftp_put that will dump the file from source to target if there is no change between source and target 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');

File won't upload to ftp server

I'm trying to upload a file from my webserver to my game server through a script. The problem is that it can't find the directory.
The full directory is /174.34.132.106 port 27015/tf/addons/sourcemod/configs/tf2items.weapons.txt
This path didn't work so I asked the hosting about it and they insisted that /tf/addons/sourcemod/configs/tf2items.weapons.txt is the correct path but this doesn't work either. The game server is running on a windows server and i'm pretty sure the web server is running on linux. Is my code wrong, do I have to replace the spaces in the directory with %20. Thanks in advance!
$ftp_server="174.34.132.106";
$ftp_user_name="Username";
$ftp_user_pass="Password";
$remote_file = "tf2items.weapons.txt";
$file = "weapons/tf2items.weapons.txt";//tobe uploaded
if(!file_exists($file)) echo "The local file does not exist";
$conn_id = ftp_connect($ftp_server) or die('Unable to create the connection');
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_chdir($conn_id, "174.34.132.106 port 27015/tf/addons/sourcemod/configs/");
echo ftp_pwd($conn_id);
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);
I noticed that the FTP server you're connecting to is using a non-standard port, so it's probably not making the connection. You need to specify the port in the ftp_connect, like so:
$ftp_server="174.34.132.106";
$ftp_port = "27015";
$ftp_user_name="username";
$ftp_user_pass="password";
$remote_file = "tf/addons/sourcemod/configs/tf2items.weapons.txt";
$file = "weapons/tf2items.weapons.txt";//tobe uploaded
// set up basic connection
$conn_id = ftp_connect($ftp_server,$ftp_port) or die('Unable to create the connection');
The die() will stop the script if it's unable to make the connection. You can add the same after your ftp_login line to make sure that it's actually logging in.
Edit To make sure your file exists, try this above the ftp_put line.
if(!file_exists($file)) echo "The local file does not exist";
Edit 2 After reading down on ftp_put, it says that the remote_file does not support directories. You'll need to use ftp_chdir first.
ftp_chdir($conn_id, "tf/addons/sourcemod/configs/");
Then for remote_file, use just tf2items.weapons.txt. You can still use filepaths for the local file.
If that's a Linux server ensure that you use correct case for directory names.
"tf/addons/sourcemod/configs" is not the same as "TF/addons/sourcemod/configs";

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