 @ May 08, 12 - 10:14 pm
As a new series to update Insane Visions and keep our readers informed, I'll be posting various things related mainly to cakePHP, PHP in general and jQuery. It may be little tidbits like this one, or may be small scripts to say work a slideshow or show/hide div's. With this one, it's a problem that I had never heard of or encountered, but was one that stumped me and a co-worker, yet the answer was a simple odd way that PHP behaves.
Uploading a file with PHP
All PHP developers at one point or another have handled file uploads. Mostly simple stuff, perhaps some in CMS's, but at one time or another all decently experienced programmers have dealth with aspects of file uploading such as max size, setting specific file types to allow and the like. There are different ways to deal with validation of both things, in a framework such as cakePHP validation is easily done in a model allowing you to deal with numerous options and conditions.
In this scenario the site was using cakePHP and my issue was that I uploaded a 3.8MB zip file (zip is not allowed) and got a flash message printed out, telling me that the format was incorrect - great. I proceeded to upload a 36MB zip file which was way over the limit set in the model of around 5.8ish MB and the result expected was a message, which was set (in two areas, actually...in the model as well as the controller) but instead, received the same exact page.
I'll ignore the boring debugging details and cut to the chase, there was no form data. I clicked submit and printing out any variation of $this->data, $_POST I received nothing. Which is odd, because the normal response should be an error or perhaps maybe the same form data (there were many other fields than the 5 uploads) but the invalid file upload containing something, instead the arrays were empty.
After looking into possibly an Apache issue and in futility, trying HTML MAX_FILE_SIZE, which does nothing productive, the root of the problem was PHP. There seems to be, not a bug, but a purposeful function in PHP that if that file like the one I had of 36MB is bigger than the server limit set in php.ini (8 MB in my case), PHP doesn't spit out an error and cancel the upload process, it actually empites out the $_POST and $_FILES array and goes along with it's business.
I'm not one to critiscize a programming language when I haven't mastered it by any means, but it seems like an odd conclusion. One would assume PHP would do about anything else then delete the form data and not say anything, at all. Luckily there is a working workaround posted on PHP.net
PHP.net Post
And here is the code below, adjusted a bit for the use I had in a cakePHP application:
if (empty($_FILES) && empty($_POST) && isset($_SERVER['REQUEST_METHOD'])
&& strtolower($_SERVER['REQUEST_METHOD']) == 'post') { $this->_successFlash('File uploaded was too big. Please re-fill in form and re-upload files under '.ini_get('post_max_size').'B'); } ?>
Basically the codebit checks to see if the $_FILES variable was empty, along with post and assuming the SERVER - REQUEST_METHOD function has a value and that it is POST, then you can assume correctly that the user uploaded a file too big and can put in whatever message or action you wish inbetween the if statement.
Well I hope this is useful for someone. It's one of those random PHP things you may not ever encounter, but luckily is a simple thing to fix.
|