# File upload error handling

**URL:** <https://discourse.slimframework.com/t/file-upload-error-handling/631>\
**Category:** Questions\
**Created:** [August 18, 2016, 11:33am UTC](https://discourse.slimframework.com/t/file-upload-error-handling/631 "2016-08-18T11:33:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rosivaldo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/rosivaldo/32/55_2.png) [@rosivaldo](https://discourse.slimframework.com/u/rosivaldo)\
**Post date:** [August 18, 2016, 11:33am UTC](https://discourse.slimframework.com/t/file-upload-error-handling/631/1 "2016-08-18T11:33:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JoeBengalen](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@JoeBengalen](https://discourse.slimframework.com/u/JoeBengalen)\
**Post date:** [August 18, 2016, 11:46am UTC](https://discourse.slimframework.com/t/file-upload-error-handling/631/2 "2016-08-18T11:46:53Z")

</div>

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()`

> <https://github.com/slimphp/Slim/blob/3.x/Slim/Http/UploadedFile.php#L273>

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

---

<div class="post-metadata">

**Author:** ![rosivaldo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/rosivaldo/32/55_2.png) [@rosivaldo](https://discourse.slimframework.com/u/rosivaldo)\
**Post date:** [August 18, 2016, 12:22pm UTC](https://discourse.slimframework.com/t/file-upload-error-handling/631/3 "2016-08-18T12:22:25Z")

</div>

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 }`

---

<div class="post-metadata">

**Author:** ![JoeBengalen](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@JoeBengalen](https://discourse.slimframework.com/u/JoeBengalen)\
**Post date:** [August 18, 2016, 1:15pm UTC](https://discourse.slimframework.com/t/file-upload-error-handling/631/4 "2016-08-18T13:15:50Z")

</div>

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:

```php
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
    }
}

```

---

<div class="post-metadata">

**Author:** ![rosivaldo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/rosivaldo/32/55_2.png) [@rosivaldo](https://discourse.slimframework.com/u/rosivaldo)\
**Post date:** [August 18, 2016, 1:44pm UTC](https://discourse.slimframework.com/t/file-upload-error-handling/631/5 "2016-08-18T13:44:48Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Susanth.Com](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/susanth.com/32/193_2.png) [@Susanth.Com](https://discourse.slimframework.com/u/Susanth.Com)\
**Post date:** [August 18, 2016, 3:29pm UTC](https://discourse.slimframework.com/t/file-upload-error-handling/631/6 "2016-08-18T15:29:28Z")

</div>

Hi @rosivaldo,

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

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

```

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

- [Err handling from Slim Doc](http://www.slimframework.com/docs/handlers/error.html)
- [Err handling Including PHP-7](https://akrabat.com/overriding-slim-3s-error-handling/)
- [Closure Assignment](http://discourse.slimframework.com/t/closure-assignment-issue-in-custom-error-handling/621)

Thank you  
Susanth K
