Google Ads

Recent Posts

Archives

Topics


« Drawing vector graphics with javascript | Main | Beware string length limitation when retrieving HTML via Ajax in Firefox! »

PHP $_FILES array mysteriously empty when uploading a file

By Shawn Bradley | July 17, 2008

Recently, a mysterious problem crept into the web application I was developing.  This particular app is a membership management tool, part of which is designed to track eligibility for membership in the organization.  This particular organization requires its members to provide documentation of their attendance at continuing education events.  This documentation is provided to the organization via uploading scanned documents into the member’s profile.

All was well, and working quite nicely, until one day files would no longer upload.  After several hours of hair-pulling, I narrowed it down to the fact that the $_FILES variable was empty after the upload form was submitted.  The big question, of course, was WHY??

After trying all of the obvious debugging techniques, all to no avail, I resolved to start with a blank slate and put my code back together one small piece at a time.  Eventually, I discovered that if I named my form something different than what it was before, the upload works, and $_FILES is not empty.  Further investigation revealed that the culprit was some of my slick Javascript UI code.

I had written a JS function designed to display a feedback GIF, and at the same time, disable the form and change its opacity so it appears grayed out.  Due to an earlier debugging session wherein IE was not disabling form controls correctly, I had commented out the code that was responsible for disabling all form controls.  After resolving that problem, I restored that code, and once again my slick form feedback UI was working marvelously.  However, this fix had the unexpected consequences of torpedoing my file uploading!

Basically, what was wrong is this: When the form controls are disabled by Javascript just prior to the form submission, PHP doesn’t recieve the form data correctly.  This resulted in the empty $_FILES array.  Removing the code that was disabling my form was all it took to get file uploading working fantastically again.  Bottom line - be very careful with slick UI tricks.  They can bite you!

Topics: Web Development, Software Development |

Comments