2017-01-02 104 views
2

我想將我的圖像存儲到特定的文件夾,並且該文件夾將按頁面ID命名。例如,如果Page id = 1,文件夾位置應該是public/pages/page_id/image_here。檢查文件夾已經存在,如果沒有在laravel中通過id創建一個新文件夾

如果該文件夾不存在,系統將生成並用其頁面標識進行命名。

$id=1; 
$directoryPath=public_path('pages/' . $id); 

if(File::isDirectory($directoryPath)){ 
     //Perform storing 
    } else { 
     Storage::makeDirectory($directoryPath); 
     //Perform storing 
    } 

但我有錯誤,「mkdir():無效的參數」。 我可以知道它爲什麼會發生?

經過我的研究,有人說文件夾名稱應該以id + token爲基礎,所以入侵者不能搜索基於id的圖片,有沒有可能實現?

+0

只是用文件: :makeDirectory存儲:: makeDirectory的實例 –

+0

可能重複[在laravel中創建文件夾](http://stackoverflow.com/questions/21869223/create-folder-in-laravel) –

回答

0

當您使用Storage外觀時,它將默認使用local磁盤,該磁盤被配置爲與storage_path()配合使用。

所以,如果你想在public目錄下創建目錄,使用File::makeDirectory這只是簡單的包裝爲mkdir()有其他選項,或直接使用mkdir():在其他條件

File::makeDirectory($directoryPath); 
mkdir($directoryPath); 
0

我有同樣的問題,但是當我用File而不是Storage它的作品!

$id=1; 
$directoryPath=public_path('pages/' . $id); 

if(File::isDirectory($directoryPath)){ 
    //Perform storing 
} else { 
    File::makeDirectory($directoryPath); 
    //Perform storing 
} 

希望這個作品!

0
For basic Laravel file system the syntax is : 
At very top under namespace write: 
use File; 

Then in your method use: 

$id=1; 
$directoryPath=public_path('pages/' . $id); 

if(File::isDirectory($directoryPath)){ 
    //Perform storing 
} else { 

    File::makeDirectory($directoryPath, 0777, true, true); 
    //Perform storing 
} 
相關問題