Asked  7 Months ago    Answers:  5   Viewed   40 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:

FileException - The file "6MB_1.jpg" exceeds your upload_max_filesize ini directive (limit is 5120 KiB).

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

 Answers

96

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',
        ])->validate();
    }

    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.)

Wednesday, March 31, 2021
 
Shreejibawa
answered 7 Months ago
72

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

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

Wednesday, March 31, 2021
 
Saxophlutist
answered 7 Months ago
32

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

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)
        {
            if(!empty($file)){
                $filename=$file->getClientOriginalName();
                $file->move(
                    base_path().'/public/uploads/', $filename
                );
            }

        }
Friday, May 28, 2021
 
Zeth
answered 5 Months ago
24

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);
fclose($input);
fclose($file);

Alternative:

$input = fopen('php://input', 'rb');
$file = fopen($filename, 'wb');
while (!feof($input)) {
    fwrite($file, fread($input, 102400));
}
fclose($input);
fclose($file);
Wednesday, July 28, 2021
 
Santi
answered 3 Months ago
88

.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.

Saturday, August 14, 2021
 
xrdty
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :