簡體   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);

我嘗試將我的遠程服務器中的文件放到另一個遠程服務器,同時我從我的localhost工作文件中輸出這個代碼,然后將文件從我的ocal pc傳輸到服務器。 但是,當我嘗試從我的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

我的測試文件夾權限是777現在該做什么,是否有任何服務器配置錯過了。

如果您完全確定該文件存在,那么您的服務器似乎有問題。 在終端中執行以下代碼可以解決您的問題:

setsebool -P httpd_can_network_connect on

它設置布爾值。 更多信息: http//linux.die.net/man/8/setsebool

我認為你的$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

您需要從兩端檢查權限:

首先,您的AWS服務器存儲桶(您當前的測試文件夾)應該對所有操作777公開(我認為您已經完成了),並且您使用的憑據必須具有訪問所有文件的所有權限。

注意:以前我的AWS服務器存在問題,我必須通過右鍵單擊文件夾手動設置文件夾公共。

要轉移的服務器上的第二件事; 對於所有用戶,文件夾或路徑應該是公共的(在傳輸所有文件后,您可以將其更改為只讀),並且所有用戶都有權在目標文件夾中寫入文件。

由@chetanAmeta編寫的代碼塊似乎是正確的。

這個答案可能對你有幫助。

因為提供賞金,當然我能夠自己解決這個問題(太糟糕了,我不能拿回我的賞金)。 但無論如何,解決方案是使用apache服務器的用戶(可能是“www-data”)掛載文件夾,然后服務器將能夠寫入共享,當然假設其余權限已設置正確的。

希望這可以幫助其他人,因為這對我來說非常令人沮喪。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM