Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_put_contents(): connect() failed: Permission denied in AWS server

$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://[email protected]/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 .

like image 246
Md Hasibur Rahaman Avatar asked Oct 20 '22 13:10

Md Hasibur Rahaman


2 Answers

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

like image 65
Neda Sharifi Avatar answered Oct 22 '22 02:10

Neda Sharifi


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.

like image 33
Ajay Gabani Avatar answered Oct 22 '22 03:10

Ajay Gabani