Last Updated: February 25, 2016
·
574
· maumagau

Force php to handle over-size file uploads

So if a user uploads a file larger than php.ini's postmaxsize, php will helpfully not post any data, at all. I say helpfully, I mean really f***ing unhelpfully. How are we supposed to let the user know the file they uploaded is too big if we don't even know they've tried POSTing it?

Here's a hack to get round it:

js - Listen for a change event on the file input. On change, add an extra parameter to the forms action.

php - If that attribute is set when the form is submitted, check that there's post data. If there isn't, the user probably tried uploading a file that was way too big.

It's not perfect, but without handling the upload via a js module, and allowing for non-html5 browsers, it's the best I've come up with so far.