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
Related
I need to transfer an file created but I keep getting the following error:
Warning: ftp_put(): Can't open data connection for transfer of "/server.txt"
I can access the ftp via an ftp application and I can create and transfer files via the application but when I try to do the same via PHP it doesn't work.
This is my code.
$serverFile = "server.txt";
$localfile = "/var/sites/store/publish/store-201908-1323-rev9286/www/text.txt";
$localfile = "text.txt";
//Connect to ftp
$ftp_server = "111.111.111.11";
$ftp_user_name = "username";
$ftp_user_pass = "password";
/*set up basic ssl connection*/
$ftp = ftp_ssl_connect($ftp_server);
// login with username and password
$login_result = ftp_login($ftp, $ftp_user_name, $ftp_user_pass);
ftp_pasv($ftp, true);
if($login_result){
if (ftp_put($ftp, $serverFile, $localfile, FTP_ASCII))
{
Mage::log("successfully uploaded \n",null,"ftp.log");
}
}
ftp_close($ftp);
Am I missing something here, I do not have access to change anything on the ftp-server but I am aware that we have remotely placed files in the server previously.
Here is also a screenshot of the Ftp-connection I am using in WinSCP. I've tried adding the port. I also tried creating a file on the server and did a ftp_get but got the same result.
I checked your code, and server.txt file will be root of path.
In general, ftp user do not have write permission on root.
Please create new directory with 777 permission and try to upload.
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);
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
Is it possible to upload a file into a subfolder on an FTP server?
I send a file daily to an FTP server, scheduled on a cron job that runs a php file. It all works fine, but now I've been asked to change the destination to a subfolder.
Changing *$server_name = 'ftp.website.com'* to *$server_name = 'ftp.website.com/data'*
doesn't seem to work?
I can see the folder exists in filezilla and it has all read/write access in permissions.
I'd be grateful if someone can advise if its a permission issue or whether it's possible at all?
Thanks in advance.
CODE...
//FTP bit...
$server_name = 'ftp.website.com';
$server_username = 'xxxx';
$server_password = 'xxxx';
$conn_id = ftp_connect($server_name);
$login_result = ftp_login($conn_id, $server_username, $server_password);
ftp_pasv($conn_id, true);
ftp_put($conn_id, $zip_filename, $output_dir . $zip_filename, FTP_BINARY);
Seems like you need to use ftp_chdir().
Connect as normal, and then change directories using ftp_chdir().
I assume you are using the ftp functions of PHP. I see two options:
1) Use ftp_put and specify the absolute path on the ftp server. http://php.net/manual/de/function.ftp-put.php
2) Before uploading your file change the working directory on the ftp server using ftp_chdir http://php.net/manual/de/function.ftp-chdir.php
If you use ftp_put you simply specify the absolute destination path on the server
http://php.net/manual/en/function.ftp-put.php
I am developing a PHP script.
I would like to transfer a remote ZIP file to a remote FTP (I'm the owner of the FTP account) via PHP.
For example:
I have two hosting accounts: a Hostgator shared account and a Hostgator VPS account.
I want to run my PHP script on the VPS hosting.
I'd like my PHP script to upload this file to the shared hosting account.
Does anyone know a PHP class for this issue?
From the manual:
<?php
$file = 'somefile.txt';
$remote_file = 'readme.txt';
// 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";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
Simply:
copy('ftp://user:pass#from.com/file.txt', 'ftp://user:pass#dest.com/file.txt');
The PHP server will consume bandwidth upload and download simultaneously.