How to upload large file > 5MB in laravel 5

后端 未结 3 1921
逝去的感伤
逝去的感伤 2020-12-03 16:34

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

相关标签:
3条回答
  • 2020-12-03 16:43

    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.

    Source: 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

    0 讨论(0)
  • 2020-12-03 16:50

    In Laravel new version (+5.3) There is a 'Automatic Streaming' ability. U can use like this:

    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

    0 讨论(0)
  • 2020-12-03 16:58

    I might be late here.

    But as i thought someone might need a way to specifically stream file to

    local storage instead of s3.

    Here is how to stream a file to local storage. As documentation says

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

    so to stream to local instead of using s3 as @koalaok mentioned in the answer you could use local. so it will be something like

    $disk = Storage::disk('local');
    $disk->put($targetFile, fopen($sourceFile, 'r+'));
    
    0 讨论(0)
提交回复
热议问题