2016-04-20 37 views
0

我正嘗試上傳帶有發佈請求的圖像並將其移至目錄。我試過..無法在目錄中寫入 - 數字海洋

$file = $request->file('file'); 
$extension = strtolower($file->getClientOriginalExtension()); 

$finfo = finfo_open(FILEINFO_MIME_TYPE); 

$generatedName = sha1(time().time()); 
$directory = '../public/uploads/imgs/'; // works 
$fileName = $generatedName . "." . $extension; // works 

// 1 
$file->move($directory, $fileName); 
// 2 
$file->move(base_path().$directory, $fileName); 

在這一點上,我收到錯誤:

1 Unable to write in the "../public/uploads/imgs" directory

2 Unable to write in the "var/www/laravel/public/uploads/imgs" directory


我以爲是權限引起的,但也沒有幫助。我試圖

sudo chmod 770 /var/www/laravel/public/uploads (also /imgs/ 
and 
sudo chmod -R 775 /var/www/laravel/public/uploads 

我還發現其他東西嘗試,但無法弄清楚用戶名位寫什麼:

sudo chown username:www-data /var/www/laravel/public/uploads/ 

我使用NGINX對數字海洋

回答

1

嘗試運行此命令給寫權限upload目錄和它的所有子目錄:

sudo chmod -R 775 /var/www/laravel/public/uploads 
+0

仍然收到相同的錯誤..這似乎沒有幫助(1)和(2) – senty

0

先嚐試創建目錄附:

$directory_path = "...";//whatever you wish it to be 

//if the directory doesn't exist already, it is created: 
if (!file_exists($directory_path)) 
{ 
    mkdir($directory_path); 
} 

起初不包括其他代碼,只是爲了看看你是否能夠實際創建一個目錄。如果它有效並且在那裏,請嘗試在那裏包含文件。讓我知道,如果這對你有用!

+0

我試過了。我在裏面放了印刷品(「A」),但是我忘記了第一次包括它。最後,我包含了'$ file-> move($ directory,$ fileName);'但是,我開始接受'無法寫入「../public/uploads/imgs」目錄中# – senty

+0

沒問題,所以如果你得到「無法寫入...」錯誤,那麼幾乎肯定是它的權限,但如果您已經將其更改爲可寫,就像您在問題中所說的那樣,那麼我無法確定。也許在你的虛擬主機提供商的論壇發佈一個調查,或者甚至打電話給他們應該工作,可悲的是它是乏味的... – Webeng

+0

葉,不幸''sudo chmod -R 775/var/www/laravel/public/uploads'不改變任何東西:( – senty

1

與開始您的錯誤消息:

1.無法寫入「../public/uploads/imgs」目錄

默認情況下,您的nginx指向您的laravel安裝的public文件夾。 ../public/uploads/imgs因此將解析爲/var/www/laravel/public/uploads/imgs,如果它沒有對laravel文件夾的讀取權限,則這將不起作用。

通常情況下,nginx只能讀取您的public文件夾。

2.無法在 「無功/網絡/ laravel /公共/上傳/ IMGS」 目錄

路徑寫是相對的。絕對路徑爲/var/www/laravel/public/var/www/laravel/public/uploads/imgs,默認情況下不存在。

解決方案:

替換這些行:

$directory = '../public/uploads/imgs/'; 
$file->move(base_path().$directory, $fileName); 

有了:

$directory = 'uploads/imgs/'; 
$file->move(public_path().$directory, $fileName); 

文檔:https://laravel.com/docs/5.1/helpers#paths

警告:不要給你nginx的寫訪問文件夾!如果有人設法上傳&覆蓋您的index.php,您的網站將被入侵。

1

請使用適當的權限創建目標路徑imgs,而不需要特別許可。請檢查以下代碼,您可以授予imgs文件夾權限。

$destinationPath = public_path().'/uploads/imgs/'; 

// Create folders if they don't exist 
if (!file_exists($destinationPath)) { 
    File::makeDirectory($destinationPath, $mode = 0755, true, true); 
} 

其中makeDirectory參數將在here澄清。

僅供參考,$模式將是0775,0777(認爲它不應該給它)。

0

須藤usermod命令-Ag WWW的數據deploybot \

須藤CHOWN -R WWW的數據:WWW的數據/無功/網絡/ laravel

須藤CHOWN -R WWW的數據:WWW的數據/無功/網絡/ laravel /公/上傳

須藤搭配chmod -R 775 /無功/網絡

它爲我 確保選中更改用戶相應 在這種情況下,它是deploybot