Asked  2 Years ago    Answers:  5   Viewed   125 times

I have my website hosted at a shared server. The maximum upload limit is 5MB. I do not have the ability to change the PHP ini settings, which is fine.

In Laravel, all the validations and file uploading works fine if the uploaded file is under 5MB. However, when a file greater than 5MB is uploaded, I get the following exception instead of a validation error:

How can I validate or force the file to be under the upload limit from server?



You don't seem interested in changing the PHP limits to allow larger files. It looks to me like you want your max upload to be 5MB, and return a proper response if it is over that.

You can handle the FileException exception inside your exception handler at app/Exceptions/Handler.php. Update the render method to add in the code you need. For example, if you'd like to return a validation exception, you will need to handle the validation inside the exception handler for the FileException exception.

public function render($request, Exception $exception)
    if ($exception instanceof SymfonyComponentHttpFoundationFileExceptionFileException) {
        // create a validator and validate to throw a new ValidationException
        return Validator::make($request->all(), [
            'your_file_input' => 'required|file|size:5000',

    return parent::render($request, $exception);

This is untested, but should give you the general idea.

You can also do client side validation via javascript, so that a file that is too large is never actually sent to your server, but javascript can be disabled or removed by the client, so it would be good to have nice server side handling set up.

For the client side validation, if you attach an event handler to the "change" event for the file input, you can check the file size using this.files[0].size, and perform any additional actions after that (disable form, remove uploaded file, etc.)

Sunday, August 21, 2022

I solved what was wrong. The code is fine, but the problem was some pictures were simply to big.

As Don't Panic pointed out, editing upload_max_filesize can solve the problem.

Tuesday, September 27, 2022

After some searching around I finally came up with a solution. First of all I modified the view to look like this:

<div class="form-group col-lg-4">
            {!! Form::label('file1', 'Select file 1', ['class' => 'control-label']) !!}
            {!! Form::file('file1', ['id'=>'file1']) !!}
        <div class="form-group col-lg-4">
            {!! Form::label('file2', 'Select file 2', ['class' => 'control-label']) !!}
            {!! Form::file('file2', ['id'=>'file2']) !!}
        <div class="form-group col-lg-4">
            {!! Form::label('file3', 'Select file 3', ['class' => 'control-label']) !!}
            {!! Form::file('file3', ['id'=>'file3']) !!}

Then in the controller I used your suggested code:

$files =[];
        if ($request->file('file1')) $files[] = $request->file('file1');
        if ($request->file('file2')) $files[] = $request->file('file2');
        if ($request->file('file3')) $files[] = $request->file('file3');
        foreach ($files as $file)
                    base_path().'/public/uploads/', $filename

Friday, December 23, 2022

Try stream_copy_to_stream, which directly pumps the content of the input into the file without copying it all into memory first:

$input = fopen('php://input', 'rb');
$file = fopen($filename, 'wb');
stream_copy_to_stream($input, $file);


$input = fopen('php://input', 'rb');
$file = fopen($filename, 'wb');
while (!feof($input)) {
    fwrite($file, fread($input, 102400));
Thursday, September 8, 2022

.Net MVC Solution:

I am using the data type of HttpPostedFileBase

In your Views > Shared Folder, create a new folder called "EditorTemplates" and use this:

@model HttpPostedFileBase

@Html.TextBox("", null, new { type = "file" })

I then pass this HttpPostedFileBase object from the controller to a method that does the following:

 public Files Upload(HttpPostedFileBase files)
    if (files.ContentLength > 0) {


The ContentLength property on the HttpPostedFileBase class contains the number of bytes in the posted file

This will make it so you have a file uploading box available.

On the ASP.NET WebForms Solution:

<asp:FileUpload ID="fuPictures" runat="server" />

Make a button with a OnClick or OnCommand event that does something like this:

if (fuPictures.HasFile == true)
    int fileSize = fuPictures.FileBytes;

That will give you the file size. Hope this helps.

Sunday, September 25, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages