File upload error handling

Hi Folks!

I’m trying to upload a file with PSR-7 file uploads. So far so good. The problem is when I’ll try to handle the errors, precisely the upload_max_filesize in php.ini.

Slim throws an exception when a submmited file is above the limit because the PHP/Apache throws an error before, and so my code is unable to fetch none of those error and send to the user.

Here’s the error:
Details

Type: Exception
Message: Expected a newfile
File: /home/user/path/to/project/src/routes.php
Line: 65

My question is, is there a way to intercept PHP/Apache error inside Slim, or handle the “Expected a newfile” exception?

Thanks in advance.

Can you show the code referred to?

I do not think Apache throws an error. If the file is too large this message will be show in $uploadedFile->getError()

Just check if that is OK before doing anything with the file.

1 Like

Hi Joe! Thanks for replying!

Well, that’s the case, on server log I have:
[Thu Aug 18 09:12:45 2016] PHP Warning: POST Content-Length of 19022680 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

And then the Slim exception when I try to access $request->getUploadedFiles(), and if I try to overcome this, and access getClientFilename(), I got a null resource.

One way I see to workaround this issue is to check if there’s a file or nor. But PHP/Apache is blocking the file.
I think I’ll use:
$files = $request->getUploadedFiles(); if ( ! empty($files['file'])) { // OK, go on! } else { // Make pretty error msg }

Yes thats a warning in the log, but where is the exception coming from?
/home/user/path/to/project/src/routes.php line 65, what there?

Of course you always have to check if the file key is available before using it, like you would with any other form field.

Going from your example:

if (isset($files['file']) && $files['file'] instanceof \Psr\Http\Message\UploadedFileInterface) {
    // not OK yet, we only know the field is set

    if ($files['file']->getError() === \UPLOAD_ERR_OK) {
        // Now we know the file is uploaded succesfully
    }
}
1 Like

Well Joe,

I’m the one who wrote the exception, I saw it on the code, and was trying to prevent someone to access directly. I’ve juste toke care of it and ripped out from my code.

But for future reference, when a user try to upload a file bigger than the specified on upload_max_filesize, the PHP/Apache doesn’t send the file reference to the script leaving the $request->getUploadedFiles() empty.

But, if you set the MAX_FILE_SIZE hidden input on your form, you can access the $request->getUploadedFiles() and then you’ll see the $uploadedFile->getError() value telling you the file is bigger.

I’ll stick with the empty/isset solution and I’ll put the instanceof clause either. Thanks again!

1 Like

Hi @rosivaldo,

Are you able to catch the error by setting custom Error handlers ?

$container['errorHandler'] = function () { /* ... */  };
$container['phpErrorHandler'] = function () { /* ... */  };

Sharing in hope, below listed related links can help in some way :

Thank you
Susanth K