A classic face-plant for many blossoming PHP devs goes something like this:
“A file upload tool? Sure, I’ve never done one but it should be a snap!”
“… oh, and a status indicator? Yeah, I’m sure someone has already written one, let me poke around the web and get something whipped right up for you…”
And that’s where the happy ends. Soon you find that while uploading a file is pretty straight forward (well, kinda, after you navigate your php ini setting and other various system based bandwidth caps) you’re going to have to sell your soul to get your hands on a decent looking status bar. So what gives? How can something this simple, be so impossible.
The problem is actually fairly fundamental and while there’s no great quick-and-dirty fix, there are several reasonable approaches to the problem. Here’s what’s killing you. When you start uploading that file the webserver starts to doing what it does best, processing a request. It takes the form data from your browser and throws it at the requested script. That form data includes your file (likely via the http POST method). While that script is running (ie. processing/uploading/etc) it isn’t going to do anything else. It certainly isn’t going to stop to let you know how much has been uploaded.
As I mentioned there are a couple of approaches:
- tell your client, friend or whoever has commissioned the work that they can’t have a status bar.
- use another tool to let the browser know how much work has been completed
- sit in the corner and cry like a little girl
Alright, so option option 1 should be out of the question. And while option 3 may just be unavoidable, we’re going to focus on #2.
Use of another tool can take nearly any form. You name it, I think I’ve seen it in my searches. From the ever-popular Flash and AJAX solutions, to CGI madness written in Perl. Even additional PHP scripts. The point is that you need something that will look at the file for you, as it grows on the server, and report it’s size back to the browser.
This is the point where I tell you that I don’t have a specific tool to recommend. This is largely because I’m still looking for the right tool myself. Rather I have a list of points to consider in your search.
- figure out who your audience is and how “standards-based” you want this to be. If you’re cool with a flash based tool (that is, you expect your users to all have flash installed) then your search is over. Something like Uploadify is your answer.
- What kind of access do you have to your server? Some hosted and shared solutions won’t give you access to your upload_max_filesize and post_max_size variables in PHP. Nor to your temp files (that’d be where the uploaded files live during upload). Figure out how much control you have and plan accordingly.
- You’re needs will likely change from project to project. I know this may seem like a no-brainer, but it can be the source of a lot of frustration. The sheer number of solutions out there will allow you to choose the best tool for the project at hand. Take advantage of that and don’t insist on reshaping the same tool for everything you do. Remember, purists die lonely ;]
With these dubious instructions in hand I’m going to turn you loose. Here are several options that I’m currently test-driving myself. Good luck.
BYO – Build your own. This guy has an interesting take that I’m currently looking into myself…
FileChucker – This guy makes the list purely on awesomeness of name. It is however just another example of how many solutions there are out there these days.