php - Laravel - 将 PHP 资源传递给 Storage::put

标签 php laravel flysystem

Laravel 文档 ( https://laravel.com/docs/5.2/filesystem#storing-files ) 声明了这一点:

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 资源”?

最佳答案

PHP 不允许您上传该大小的文件。文档中指出的资源是这种 PHP resource

以下是使用 Intervention 从外部图像 URL 创建图像的简单示例。 Intervention库使用GD库,该库位于PHP资源列表下。

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

关于php - Laravel - 将 PHP 资源传递给 Storage::put,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38595304/

相关文章:

PHP 生成 00 - 29 之间的随机数

php - 在 Laravel 中 use User; 之间有什么区别?和/用户

php - Laravel 5.1 Mail::queue Amazon SQS 错误 404

javascript - Laravel Ajax - 未捕获的 ReferenceError : KWS1389776 is not defined at HTMLButtonElement. onclick

laravel - 如何在 Laravel 存储上启用 "reduced redundancy"

php - Laravel 文件系统 sftp 缓存适配器

php - Laravel 5.5 - 检查字符串是否包含确切的单词

php - Symfony 表单 : The selected choice is invalid

php - 在 .htaccess 中为 php 文件创建重写规则

laravel - 如何在 Laravel 中测试同时使用 `Storage::put()` 和 `Storage::temporaryUrl()` 的路由?