-1

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.

enter image description here

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
Natalie Hedström
  • 2,607
  • 3
  • 25
  • 36
  • Can upload the file using an FTP client running on the *same machine* as your PHP code? + Did you try `FTP_USEPASVADDRESS`? See [PHP ftp_put fails](https://stackoverflow.com/q/40720260/850848). – Martin Prikryl Jan 26 '23 at 14:46
  • How do you mean I would go about doing that? I have only tried uploading the file via the PHP code and then via WinSCP on my local windows maching. – Natalie Hedström Jan 26 '23 at 14:50
  • @MartinPrikryl I tried adding `FTP_USEPASVADDRESS` but then I got the following error instead `Notice: Use of undefined constant FTP_USEPASVADDRESS` – Natalie Hedström Jan 26 '23 at 14:58
  • We do not know anything about the machine where your PHP code runs. Is that web server? Do you have a shell access there? + If `FTP_USEPASVADDRESS` is undefined, then you are possibly using some rather old version of PHP. – Martin Prikryl Jan 26 '23 at 15:21
  • So the machine where the PHP code runs is a Centos 6 and it's running with php5 so you are right that it is a old version of php unfortunately and that is out of my hands to change. I have full access to the server that we run the php code but not to the remote FTP server. Is there any other information that could be useful to help find the issue? – Natalie Hedström Jan 27 '23 at 06:00
  • Then try to use command-line `ftp` client on the Centos to upload a file to the FTP. Post verbose log of that client when you succeed. – Martin Prikryl Jan 27 '23 at 07:08
  • Thank you for trying to help but due to us being on a time crunch with this I had to find a different solution for now where we set up our own ftp which we have full access to and can transfer files to instead. – Natalie Hedström Jan 27 '23 at 09:44

1 Answers1

0

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.

bluestar0505
  • 338
  • 1
  • 14
  • Please correct me if I am wrong because I haven't really worked a lot with ftp-servers but when I connect with a specific user will it not generally send me to the specific folder set up on the destination server for the ftp? We have previously added files to the ftp with that user and as I mentioned I can also logon via an ftp-client and add files without any issues. – Natalie Hedström Jan 26 '23 at 14:38
  • For example, you can create 'ftp_files' director on '/home/[your_user]. And set 777 permission. $serverFile = "/home/[your_user]/server.txt"; – bluestar0505 Jan 26 '23 at 14:52
  • And I use league/flysystem-ftp package for ftp uploading. And if you are okay I can help you on remote. https://flysystem.thephpleague.com/docs/adapter/ftp/ – bluestar0505 Jan 26 '23 at 14:58