Laravel - Passing a PHP resource to Storage::put

血红的双手。 提交于 2019-12-06 04:57:46

问题


The Laravel docs (https://laravel.com/docs/5.2/filesystem#storing-files) state this:

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

I am looking to save a file larger than my php memory limit (512MB), so when I do this, I get a memory error:

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

How do I use the streaming functionality as indicated in the docs? How do I go from a file path to a "PHP resource"?


回答1:


PHP is not allowing you to upload a file with that size. The resource that is being pointed out in the docs are this kind of PHP resource

Here's a simple example of creating an image from an external image URL using Intervention. The Intervention library uses the GD library, which is under the PHP resource list.

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

In your case, here's an example code on uploading a file.

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


来源:https://stackoverflow.com/questions/38595304/laravel-passing-a-php-resource-to-storageput

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!