PHP File Uploads

Yesterday at work I spent the better part of the afternoon trying to figure out why a form couldn’t handle multiple file uploads. These uploads where failry big – quicktime and wmv files – so I thought the culprit was the large file size. But the script was not returning any error warnings or notices which made it quite a pain to debug. With a co-workers help we traced it to two settings in php.ini.

  • post_max_size = (some number)M
  • upload_max_filesize = (some number)M

post_max_size controls the total size of your post request while upload_max_filesize controls the max size of each file uploaded. The sum of file sizes can’t exceed the maximum size of the post request.

A related apache setting could come in to play here, LimitRequestBody, which also sets a limit on how large a post request can be. So, in order to have working uploads for your php scripts, these 3 numbers have to jive. LimitRequestBody = post_max_size >>> upload_max_filesize

PHP Application Frameworks

Whenever I’ve read about projects or people advocating the use of a Model-View-Controller framework for a php-driven web site, I’ve always gotten a hunch that its an overly-big hammer to use for your typical website. While the theory of its use may make sense for a desktop client application, I’m not convinced it fits for a website where what you are doing is sending pages of content to a browser. Other people seem to share this viewpoint. See Web Applications: still no ideal architecture… which links to the more interesting The Mountain of Worthless Information who writes:

At the end of the day, we need to stop pretending that the Web browser is just an extension of the desktop, and recognize it for what it really is: the old mainframe terminal, gussied up by lots of colors, graphics, and better fonts. Treat your web apps as we did terminal-based apps 30 years ago, and you’ll find your system behaves a lot better as a result.

On a related note, while the MVC pattern may not work for a php webpage. There’s no reason not to use a Frontcontroller. The WACT project’s page on FrontControllers is a handy reference for all the different ways you can register such a script on Apache including: Physical mapping, using mod_rewrite, and using the ErrorDocument directive.

Turns out all this time when I’ve been coding I’ve been using FrontControllers, I just didn’t know it. I see some interesting ways I can automate what I’ve been writing to create a simple, reusable front-controller script.