2016-07-26 56 views
3

的Laravel文檔(https://laravel.com/docs/5.2/filesystem#storing-files)狀態這樣的:Laravel - 傳遞一個PHP資源存儲::把

Storing Files

The put method may be used to store a file on disk. You may also pass a PHP resource to the put method, which will use Flysystem's underlying stream support. Using streams is greatly recommended when dealing with large files:

Storage::put('file.jpg', $contents);

Storage::put('file.jpg', $resource);

我期待保存文件比我的PHP內存限制(512MB)大,所以當我做到這一點,我得到一個內存錯誤:

FatalErrorException in Local.php line 128: Allowed memory size of 536870912 bytes exhausted (tried to allocate 377028088 bytes).

如何使用文檔中指出的流功能?我如何從文件路徑轉到「PHP資源」?

回答

2

PHP不允許您上傳具有該大小的文件。正在文檔中指出的資源是PHP resource

下面是使用Intervention從外部圖像URL創建圖像的一個簡單示例。干預庫使用位於PHP資源列表下的GD庫。

$image = Image::make('Your Extenal URL')->stream(); 
Storage::put('image_name.jpg', $image->getContents()); 

對於您的情況,這裏是上傳文件的示例代碼。

$file = Request::file('file_field'); 
Storage::put($file->getClientOriginalName(), File::get($file)); 
+0

真的很酷,並保存我從文件流 – Michael