Laravel-将PHP资源传递到Storage :: put [英] Laravel - Passing a PHP resource to Storage::put
问题描述
Laravel文档( https://laravel.com/docs/5.2/filesystem#存储文件)声明如下:
The Laravel docs (https://laravel.com/docs/5.2/filesystem#storing-files) state this:
存储文件
put方法可用于将文件存储在磁盘上.您也可以将PHP资源传递给put方法,该方法将使用Flysystem的基础流支持.在处理大文件时,强烈建议使用流:
Storage::put('file.jpg', $contents);
Storage::put('file.jpg', $resource);
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)的文件,所以当我这样做时,出现内存错误:
I am looking to save a file larger than my php memory limit (512MB), so when I do this, I get a memory error:
Local.php第128行中的FatalErrorException:耗尽的内存大小为536870912字节(尝试分配377028088字节).
FatalErrorException in Local.php line 128: Allowed memory size of 536870912 bytes exhausted (tried to allocate 377028088 bytes).
如何使用文档中指示的流功能?如何从文件路径转到"PHP资源"?
How do I use the streaming functionality as indicated in the docs? How do I go from a file path to a "PHP resource"?
推荐答案
PHP不允许您上传具有该大小的文件.文档中指出的资源是这种 PHP资源
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
这是使用干预从外部图像URL创建图像的简单示例.干预库使用GD库,该库位于PHP资源列表下.
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));
这篇关于Laravel-将PHP资源传递到Storage :: put的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!