如何在 Laravel 5 中上传大于 5MB 的大文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36600197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to upload large file > 5MB in laravel 5
提问by Zeeweesoft
May I know how to upload large file, more than 5Mb in Laravel 5? I am trying to upload around 10MB image file but it is not uploading, I searched a lot and updated following settings in php.ini
我可以知道如何在 Laravel 5 中上传超过 5Mb 的大文件吗?我正在尝试上传大约 10MB 的图像文件,但它没有上传,我搜索了很多并更新了以下设置php.ini
post_max_size = 500M;
memory_limit = 500M;
upload_max_filesize = 500M
I have restarted Apache server but still having issue. Is there any other setting in php.ini
or in Laravel to upload large files? Please help.
我已经重新启动了 Apache 服务器,但仍然有问题。php.ini
Laravel中或中是否有其他设置可以上传大文件?请帮忙。
Edit
编辑
$image = $request->file('image');
if($image && $image != '') {
$extension = $image->getClientOriginalExtension();
$imageName = $this->getUniqueName($extension);
$image->move('gimages/profile_images/', $imageName);
$profile_image = $imageName;
$update_user_data['image'] = $profile_image;
}
Thanks much!
非常感谢!
回答by aimme
I might be late here.
我可能来晚了。
But as i thought someone might need a way to specifically stream file to
但是因为我认为有人可能需要一种方法来专门将文件流式传输到
local storage instead of s3.
本地存储而不是 s3。
Here is how to stream a file to local storage. As documentationsays
这是将文件流式传输到本地存储的方法。正如文档所说
The Local Driver
When using the local driver, note that all file operations are relative to the root directory defined in your configuration file. By default, this value is set to the storage/app directory. Therefore, the following method would store a file in storage/app/file.txt:
Storage::disk('local')->put('file.txt', 'Contents');
本地司机
使用本地驱动时,注意所有文件操作都是相对于你的配置文件中定义的根目录。默认情况下,此值设置为 storage/app 目录。因此,以下方法会将文件存储在 storage/app/file.txt 中:
Storage::disk('local')->put('file.txt', 'Contents');
so to stream to local instead of using s3as @koalaok mentioned in the answer you could use local. so it will be something like
所以要流式传输到本地而不是使用s3作为答案中提到的@koalaok,您可以使用local。所以它会像
$disk = Storage::disk('local');
$disk->put($targetFile, fopen($sourceFile, 'r+'));
回答by koalaok
If you want to upload big files you should use streams. Here's the code to do it:
如果你想上传大文件,你应该使用流。这是执行此操作的代码:
$disk = Storage::disk('s3');
$disk->put($targetFile, fopen($sourceFile, 'r+'));
PHP will only require a few MB of RAM even if you upload a file of several GB.
即使您上传了几 GB 的文件,PHP 也只需要几 MB 的 RAM。
Source: https://murze.be/2015/07/upload-large-files-to-s3-using-laravel-5/
来源:https: //murze.be/2015/07/upload-large-files-to-s3-using-laravel-5/
See Lrvl5 doc for usage and config of Storage : https://laravel.com/docs/5.0/filesystem
有关存储的使用和配置,请参阅 Lrvl5 文档:https://laravel.com/docs/5.0/filesystem
回答by Saman Sattari
In Laravel new version (+5.3) There is a 'Automatic Streaming' ability. U can use like this:
在 Laravel 新版本 (+5.3) 中有一个“自动流式传输”的能力。你可以这样使用:
use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
// Automatically generate a unique ID for file name...
Storage::putFile('photos', new File('/path/to/photo'));
// Manually specify a file name...
Storage::putFileAs('photos', new File('/path/to/photo'), 'photo.jpg');
For more information please follow this docs and search for 'Automatic Streaming': https://laravel.com/docs/5.6/filesystem
有关更多信息,请遵循此文档并搜索“自动流媒体”:https: //laravel.com/docs/5.6/filesystem