7

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

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.

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!

Sachin Vairagi
  • 4,894
  • 4
  • 35
  • 61
  • 1
    You need to be more specific than **"it is not uploading"**. Please post the form that is initiating the upload, as well as the code that is responsible for processing the upload and explain what exactly is not working with it. – Bogdan Apr 13 '16 at 13:40
  • Do you receive any error? – Jerodev Apr 13 '16 at 13:43
  • No, when I print request in "Advanced Rest Client" to see POST data, it didn't show any data even other input fields @Jerodev – Sachin Vairagi Apr 13 '16 at 13:55
  • @SachinVairagi Do you see any post data? Try passing a text field as well, if you can't see this when you print the request object, then it is probably a rewrite issue as apposed to the upload. – John Doe Apr 13 '16 at 13:57
  • @JohnDoe It doesn't show any post data when I submit large file, but when submit small file around 1MB, it prints post data and upload file successfully as well. – Sachin Vairagi Apr 13 '16 at 14:01
  • @SachinVairagi from experience this would suggest your app is crashing before a response is returned. What OS are you using? – John Doe Apr 13 '16 at 14:39
  • @JohnDoe, I am using ubuntu 14.04 but testing web service in Advanced Rest Client – Sachin Vairagi Apr 14 '16 at 12:45
  • Do not ever use `getClientOriginalExtension` unless you know what you're doing. This could be changed easily by the client. https://stackoverflow.com/questions/38403558/get-an-image-extension-from-an-uploaded-file-in-laravel/48979159?noredirect=1#comment96306942_45887547 – Amir Hossein Abdollahi Oct 01 '19 at 21:13

3 Answers3

11

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

koalaok
  • 5,075
  • 11
  • 47
  • 91
  • I don't want to upload file on Amazon s3, I just want to upload it on our own server. – Sachin Vairagi Apr 14 '16 at 12:47
  • s3 is the config name in filesystem. You can call it anyway you like. See Storage doc on how to configure a private FTP server and use it seamlessly with Storage. – koalaok Apr 14 '16 at 12:49
11

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+'));
aimme
  • 6,385
  • 7
  • 48
  • 65
  • 1
    Thank you for your reply @aimme , definitely it will help others. – Sachin Vairagi Mar 20 '17 at 10:33
  • What about Google drive? I am getting error while trying to upload in Google drive.But works fine for local storage. https://stackoverflow.com/questions/46124539/laravel-file-storage-error – Asm Arman Sep 09 '17 at 12:32
1

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

Saman Sattari
  • 3,322
  • 6
  • 30
  • 46