简体   繁体   English

file_put_contents():connect()失败:AWS服务器中的权限被拒绝

[英]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. 我尝试将我的远程服务器中的文件放到另一个远程服务器,同时我从我的localhost工作文件中输出这个代码,然后将文件从我的ocal pc传输到服务器。 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 但是,当我尝试从我的AWS服务器中删除此代码时,它不会传输任何文件,当我检查其给出的错误日志文件时

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 . 我的测试文件夹权限是777现在该做什么,是否有任何服务器配置错过了。

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 更多信息: http//linux.die.net/man/8/setsebool

I think issue with your $hostname try it with: (I tried this solution with my ec2 instance and its working.) 我认为你的$hostname问题可以尝试:(我试过这个解决方案,我的ec2实例和它的工作。)

<?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 source: PHP:file_put_contents - Manual

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. 首先,您的AWS服务器存储桶(您当前的测试文件夹)应该对所有操作777公开(我认为您已经完成了),并且您使用的凭据必须具有访问所有文件的所有权限。

Note: previously I have an issue with AWS server that I have to set folder public manually by right clicking on folder. 注意:以前我的AWS服务器存在问题,我必须通过右键单击文件夹手动设置文件夹公共。

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. 由@chetanAmeta编写的代码块似乎是正确的。

This answer might help you. 这个答案可能对你有帮助。

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. 但无论如何,解决方案是使用apache服务器的用户(可能是“www-data”)挂载文件夹,然后服务器将能够写入共享,当然假设其余权限已设置正确的。

Hope this helps someone else out, as it was very frustrating for me in the beginning. 希望这可以帮助其他人,因为这对我来说非常令人沮丧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 file_put_contents无法打开流:权限被拒绝 - file_put_contents failed to open stream: Permission denied file_put_contents - 无法打开流:权限被拒绝 - file_put_contents - failed to open stream: Permission denied file_put_contents无法打开流:权限被拒绝 - file_put_contents failed to open stream: Permission denied Codeigniter file_put_contents:未能打开 stream:权限被拒绝 - Codeigniter file_put_contents: failed to open stream: Permission denied 警告:file_put_contents():无法打开流:权限被拒绝 - Warning: file_put_contents(): failed to open stream: Permission denied Vagrant file_put_contents权限被拒绝 - Vagrant file_put_contents permission denied file_put_contents权限被拒绝+ livedocx - file_put_contents permission denied + livedocx file_put_contents(文件路径/文件)[function.file-put-contents]:无法打开流:权限被拒绝 - file_put_contents(filepath/file) [function.file-put-contents]: failed to open stream: Permission denied Google 云端硬盘:file_put_contents(token.json):无法打开流:权限被拒绝 - Google Drive: file_put_contents(token.json): failed to open stream: Permission denied Symfony 控制台命令 v6 和 Dompdf --- file_put_contents - 无法打开 stream:权限被拒绝 (windows) - Symfony Console Commands v6 and Dompdf --- file_put_contents - Failed to open stream: Permission denied (windows)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM