php big upload via ftp - php

i have to trasfer file via ftp with php. The files are big (also over 500MB).
So i think use php with ftp.
<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "localfile.txt";
// upload file
if (ftp_put($ftp_conn, "serverfile.txt", $file, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
?>
I have to know if the timeout is considered also in ftp trasfer.
If i trasfer the file via php page like upload.php with the code write, i have the execution time limit setting on the web server ?

You must take too many considerations before doing this operation, as mentioned below the first thing you need to check if your FTP server allows 500MB for uploads, the second thing you must set some PHP directives like post_max_size and max_upload_size, I suggest to use an FTP library to make it easier to do that, this a great one php-ftp-client that I've build.

Enable the feature on server side, which allows resuming file uploads.
From php you might be able to call the ftp with command_line options using functions like system(), or exec().
And, yes, in php there are limits: max_upload_size, max_execution_time, max_post_size and etc.
You will have to split the whole process into steps, with reloading of the page - if needed.

Related

Transfer files between two remote FTP servers in PHP

First of all, I know that this a duplicate topic, but the other post that I found were not useful for my situation, so I decided to create a new one.
What I'm trying to accomplish is to get a file from one FTP server and upload it to another FTP server.
I'm using this code:
$ftp_server = "ftp_server";
$ftp_user_name = 'ftp_username' ;
$ftp_user_pass = 'ftp_pass' ;
$localDir = "full/path/";
$serverDir = "full/path/";
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_get($conn_id, $localDir, $serverDir, FTP_BINARY)) {
// ftp_fput($conn_id, $file, $fp, FTP_ASCII))
}
The problem that I have is when you use ftp_put command, it requires a local file, but this file it's not on my computer, so I can't upload it to the other ftp.
Is there a way to upload the file that I just got with ftp_get function into another server using ftp_put? Without the need to download it first on your PC?
Thanks!
Both ftp_get and ftp_put can operate with files only, not folders.
Use ftp_get to download a file from the first server to a local temporary folder/file. And then use ftp_put to upload the temporary file to the second server.
If you want to avoid using a temporary file, you can download the file to memory using ftp_fget and re-upload to the second server using ftp_fput.
PHP: How do I read a .txt file from FTP server into a variable?
Transfer in-memory data to FTP server without using intermediate file

How to use FTP to connect to Azure scale set Instance

So i have created a scale set in Azure (2 windows server 2016 VMs). I want to have a PHP application running on them. I want to know if it is possible to use an FTP connection to remotely upload/edit my php files which are going to be on the VMs. If not, what are the others ways i can use to remotely edit/upload my php files?. This is my first time working with a scaleset. thanks
Yes it's possible to use FTP in php:
//open a connection
$conn = ftp_connect($host);
//login to server
ftp_login($conn, $user, $pass);
//upload file
if (ftp_put($conn, $remoteFile, $localFile, FTP_ASCII)) {
return "<p>successfully uploaded $localFile to $remoteFile</p>";
} else {
return "<p>There was a problem while uploading $remoteFile</p>";
}
See the manual for more FTP functions http://php.net/manual/en/book.ftp.php

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 download failed with "failed to open stream: Error downloading"

I am trying to download from FTP server (using FileZilla server) to my local computer, but I keep getting error
This is my code
<?php
// connect and login to FTP server
$ftp_server = "127.0.0.1";
$ftp_username = "myusername";
$ftp_userpass = "mypassword";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
echo "success connect to FTP";
$local_file = "C:\Users\PROSIA\Desktop\test.docx";
$server_file = "C:\Users\PROSIA\Documents\dummyfile.docx";
// download server file
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII))
{
echo "Successfully written to $local_file.";
}
else
{
echo "Error downloading $server_file.";
}
ftp_close($ftp_conn);
This is the error I get:
Warning: ftp_get(C:\Users\PROSIA\Desktop est.docx): failed to open stream:
Error downloading C:\Users\PROSIA\Documents\dummyfile.docx.
My logic for the code is $local_file is the path to save the downloaded file to my local computer and $server_file is the path from FTP server to download the file
So I am a bit confused with the first warning, "failed to open stream" while the file is not exist yet and its seem got blank space (it should be \Desktop\test.docx rather than Desktop est.docx)
And additional question, can I just read, without download?
You cannot use absolute paths to remote files in FTP protocol.
The FileZilla FTP servers has mapping in its configuration that projects the remote file system into virtual FTP file tree. You have to use paths to the virtual file tree.
E.g. The C:\Users\PROSIA can be mapped to something like /users/PROSIA. In that case you use:
$server_file = "/users/PROSIA/dummyfile.docx";
Chances are that you have actually not configured the mapping at all. So you cannot really access the file, until you do the mapping.
Start by connecting to the FTP server with some GUI FTP client, and try to locate the file. The client will show you the correct path to use in your code.
The next problem you have, is that you need to enable the FTP passive mode. The default active mode will hardly work, if you are behind a firewall on NAT. See my article on network configuration needed for active and passive FTP modes.
To switch to the passive mode, use the ftp_pasv function.
ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
ftp_pasv($ftp_conn, true);
And yes, you can read without downloading.
See
PHP: How do I read a .txt file from FTP server into a variable? or
Stream FTP download to output

PHP ftp_put warning Warning: ftp_put() [function.ftp-put]: Type set to I. in

When i try to upload files using PHP's ftp_put function, earlier it was erroring:
Warning: ftp_put() [function.ftp-put]: No data connection
Now, i tried to put passive mode on:
ftp_pasv($conn_id, true);
then comes error:
Warning: ftp_put() [function.ftp-put]: Type set to I. in
ftp_login is done properly and it says Successfully.
Now it gives new warning: Warning: ftp_put() [function.ftp-put]: abc.txt: Cannot open or remove a file containing a running program.
Any ideas, why file not tranferring ?
Thanks !
Here is my code snippet:
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("You do not have access to this ftp server!");
if ((!$conn_id) || (!$login_result)) {
// wont ever hit this, b/c of the die call on ftp_login
echo "<span style='color:#FF0000'><h2>FTP connection has failed! <br />";
echo "Attempted to connect to $ftp_server for user $ftp_user_name</h2></span>";
exit;
} else {
//echo "Connected to $ftp_server, for user $ftp_user_name <br />";
}
//turn passive mode on
ftp_pasv($conn_id, true);
$upload = ftp_put($conn_id, $destination_file.$name, $filename, FTP_BINARY);
if (!$upload) {
echo "<span style='color:#FF0000'><h2>FTP upload of $filename has failed!</h2></span> <br />";
} else {
echo 'Uploaded';
}
ftp_close($conn_id);
http://php.net/ftp_pasv
$resource = ftp_connect('ftp.example.com');
ftp_login($resource, 'username', 'password');
# set this to true
ftp_pasv($resource, true);
ftp_get(...);
ftp_put(...);
I was recieving same (not very descriptive) error message E_WARNING ftp_get(): Type set to I..
I found out that it is because server running PHP did not have visible public IP (it is virtual server on my workstation).
Solution was using passive mode. Default setting (active mode) did not have problem on live server, because live server has visible public IP.
The last error you are seeing happens when the FTP daemon is stuck with the uploaded file open and waiting for you to write to it.
Anytime you successfully open a connection over an FTP server, be prepared to close the connection with the following function when the process completes or terminates due to any errors.
ftp_close($conn_id);
It's possible your script is leaving its connections open and the FTP server is getting confused by this. Try adding ftp_close in the appropriate places and see if the script runs more smoothly.
I've tried using the ftp functions in PHP and found it was much easier to use file_put_contents() like the following:
$remote_file = "ftp://username:password#host.com/path/to/file.txt";
file_put_contents($remote_file, $file_contents);
You can still check if it was successful and all that good stuff of course too.
Your ftp setup looks ok, try putting the filename $destination_file.$name in a single variable, dump the variable and make sure this file exists with absolute path if it is not in the same folder as your script. That is the only detail I saw in a quick glance, that could choke your upload.
Make sure your file is not opened in an editor! And if the file is .txt you can use FTP_ASCII although being in binary should not cause a problem.
Good-luck!
I found its solution as below:
I just talked to EUKHOST server support
Main point in this was that the support person now opened a passive port range for FTP on server, and he told us to try the FTP upload now. If you could try it with some testfile and it went through successfully..
Add following lines at the end of
open /etc/vsftpd.conf and add
pasv_promiscuous=YES___ at the end.
In my case, the issue triggering this error was that the file I was trying to upload was too large for the recieving server's configuration.

Categories