Send file to ftp-server - php

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.

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

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

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.

Connect to Server via PHP and Write File

I am trying to connect to a server via php and write a PDF file to a folder that I dynamically create. This is the code I am using:
<?php
$ftp_server = "[server ip]";
$ftp_user_name = "[username]";
$ftp_user_pass = "[password]";
// set up basic ssl connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
if ($login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)){
mkdir('/home/luke/public_html/pdfs/kmhllc/', 0777, true);
}else{
echo "Connection Failed";
}
// close the ssl connection
ftp_close($conn_id);
?>`
The code connects to the server just fine but it can't write to the folder even though it has 0777 permissions on the server.
use ftp_mkdir not mkdir to create a directory on the ftp server.

FTP to a subfolder

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

Categories