Debugging uploads in Laravel

Nav • August 27, 2020

laravel

Recently while debugging a file upload issues, i came accross a few helper methods that can save a lot of time.

Uploaded file returns a instant of Illuminate\Http\UploadedFile, which extends symfony's SymfonyUploadedFile

isValid()

Returns whether the file was uploaded successfully.

$request->file('file')->isValid();
getError()

Returns the upload error. If the upload was successful, the constant UPLOAD_ERR_OK is returned. Otherwise one of the other UPLOAD_ERR_XXX constants is returned.

$request->file('file')->getError();

This will return an error code. Here is the list of error codes:

Code Meaning
0 There is no error, the file uploaded with success
1 The uploaded file exceeds the upload_max_filesize directive in php.ini
2 The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form
3 The uploaded file was only partially uploaded
4 No file was uploaded
6 Missing a temporary folder
7 Failed to write file to disk
8 A PHP extension stopped the file upload
getMaxFilesize()

Returns the maximum size of an uploaded file as configured in php.ini. This is especially helpfull to check if your php.ini settings are being respected.

$request->file('file')->getMaxFilesize();
getErrorMessage()

Returns an informative upload error message.

$request->file('file')->getErrorMessage();