1

Within Laravel, I am successfully able to have a user upload a file on a page, but I want to know if there is a way for there to be an error displayed to that user before they submit the page that the file is too big. Something like "The file you selected to upload is 25MB. Get it below 20MB."

Is there some kind of package that can handle this?

1
  • 1
    Take a look at Laravel's built in validation: laravel.com/docs/5.7/validation. They have validation for files where you can check the type and size. Commented Feb 20, 2019 at 16:26

2 Answers 2

2

Validating the file size on client side. (Mentioning this because you have mentioned that you would like to alert the error before form submit.) Check the example code below which uses jQuery :

$(document).ready(function() {

    $('input[type="file"]').change(function(event) {
        var fileSize = this.files[0].size;
        var maxAllowedSize = //add your value here;
        // check the file size if its greater than your requirement
        if(size > maxAllowedSize){
          alert('Please upload a smaller file');
          this.val('');
        }

    });
});

Validation on server side(you can change mime types as per the file type you want to allow) :

<?php 

public function store(Request $request){

    $request->validate([
        'file_input_name' => 'file|max:25000|mimes:jpeg,bmp,png',
        // add validations for other fields here
    ]);
}

For more check documentation

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. This is exactly what I needed. I needed to change this.val(''); to $(this).val(''); just fyi. And if(fileSize... instead of if(size... for anyone else looking at this.
0

You don't need a package to do this, you can create either a Request class or use a validator:

1. Create a Request class:

Run the command php artisan make:request FileRequest

Then, on the File generated under App\Http\Requests\FileRequest do the following:

  • Change the authorize method to return true instead of false.
  • Under the rules method you return your validation rules:

return [ "file_input" => "max:20480", //If your input type's file name is "file_input" ];

According to documentation, max rule verifies that the input size from the user will not exceed the specified number in kilobytes for files.

2. You can also create a validator in your controller method:

use Validator;

public function store(Request $request)
    {
        $validator = Validator::make($request->only('file_input'), [
            'file_input' => 'max:20480',
        ]);

        if ($validator->fails()) {
            return redirect()
                        ->route('your.route.name')
                        ->withErrors($validator)
                        ->withInput();
        }

        // other code here
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.