5
$username_ftp = "user";
$password_ftp = "password";
$file_name = "remote-transfer.txt";
$url = "example.biz/test/" . $file_name;
$hostname = "ftp://$username_ftp:$password_ftp@$url";
$content = file_get_contents('index.html');
$options = array('ftp' => array('overwrite' => true));
$stream = stream_context_create($options);
file_put_contents($hostname, $content, 0, $stream);

I try to put a file from my remoter server to another remote server while i exixute this code from my localhost it work file and i transfer a file from my ocal pc to server. But When i try to exicute this code from my AWS server it not transfer any file , When i check my error log file its gives

PHP Warning:  file_put_contents(): connect() failed: Permission denied 
PHP Warning:  file_put_contents(ftp://...@example.biz/remote-transfer.txt): failed to open stream: operation failed in

my test folder permission is 777 now what to do, is there any server configuration which is miss to do .

Md Hasibur Rahaman
  • 1,041
  • 3
  • 11
  • 34

4 Answers4

2

If you're absolutely sure that the file exists, it seems a problem with your server. Executing the code below in terminal could solve your problem:

setsebool -P httpd_can_network_connect on

It sets the boolean value. More info: http://linux.die.net/man/8/setsebool

Neda Sharifi
  • 542
  • 5
  • 6
1

You need to check privileges from both end:

first your AWS server bucket(your current test folder) should be public for all operations 777(I think which you already done) and the credentials you are using must have all privileges to access all files.

Note: previously I have an issue with AWS server that I have to set folder public manually by right clicking on folder.

Second thing on server on which you want to transfer; The folder or path should be public for all users(After transfer all file you can change it to read only) and with this all user has permission to write file in your targeted folder.

And code block written by @chetanAmeta seems correct.

This answer might help you.

Ajay Gabani
  • 1,168
  • 22
  • 38
  • Thanks, I used Filezilla to change the permissions for the directory and it worked. – Rog Sep 05 '18 at 23:40
0

I think issue with your $hostname try it with: (I tried this solution with my ec2 instance and its working.)

<?php 
/* set the FTP hostname */ 
$user = "test"; 
$pass = "myFTP"; 
$host = "example.com"; 
//path of file
$file = "test.txt"; 
$hostname = $user . ":" . $pass . "@" . $host . "/" . $file; 

/* the file content */ 
$content = "this is just a test."; 

/* create a stream context telling PHP to overwrite the file */ 
$options = array('ftp' => array('overwrite' => true)); 
$stream = stream_context_create($options); 

/* and finally, put the contents */ 
file_put_contents($hostname, $content, 0, $stream); 
?>

source: PHP: file_put_contents - Manual

Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44
0

Since offering a bounty, of course I was able to figure it out on my own (too bad I can't take my bounty back). But anyways, the solution is to mount the folder using the user of the apache server (probably "www-data"), and then the server will be able to write to the share, of course given that the rest of the permissions are set up properly.

Hope this helps someone else out, as it was very frustrating for me in the beginning.

Brian Leishman
  • 8,155
  • 11
  • 57
  • 93
  • Hi, hope you can help me but i have the same issue and i happen to agree that your solution is correct, my question is how do you mount a folder using www-data user? Thanks in advance –  May 30 '17 at 02:53
  • 1
    @MainaWycliffe You have to create the `www-data` user on the target samba server and connect to it with the new credentials – Brian Leishman May 30 '17 at 13:08