Saturday, June 16, 2007

What's new in PHP V5.2, Part 5: Tracking file upload progress

PHP V5.2 added hooks for developers to take advantage of tracking file upload progress in real time. This article, Part 5 of a five-part "What's new in PHP V5.2" series, shows how to monitor file uploads and write code accordingly, with the creation of a PHP progress bar.

Web 2.0 is the hottest buzzword on the Internet, with investors lining up to put money into any bucket labeled by it. There are many descriptive terms for the millions of Web sites and applications they house. With Web 2.0, we describe a category of Web sites that provide a voice to the millions of users of the Internet. Distinctive in that they all provide a venue for users to meet and share opinions and data relating to common interests, these sites generate enormous amounts of content rapidly.

Each of the users provides some kind of content -- reviews of coffee shops, routes to work, etc. YouTube is a great example of this, providing a place for people to upload videos and have other users watch them and provide feedback. YouTube is the current darling of the Web 2.0 observers, noting that YouTube's popularity grew faster than any site on the Internet to date. This popularity can be attributed to a great deal of varied content, along with the ability for users to lend their voices to the content in the form of comments. And not just comments -- users can even upload video comments in response to video.

Text fields, ahoy

Many Web sites that accept files sport the dreaded Browse button next to a text field urging users to upload each file one at a time. This can take a long time, especially in the case of video, or even photos or other items that come in a groups of smaller files. Since each file requires its own upload, it can be quite a chore. Given that uploading huge files can be tedious for impatient users, it is important to provide them positive feedback to keep them from giving up and going away.

Fortunately, PHP V5.2's new hooks into the file upload process allow us to show users in real time what is happening with their uploads. In this article, we will create a progress bar using PHP V5.2 for our users (see Download for source code).





Hook, line, and sinker

The new "hooks" in PHP V5.2 are actually data points that are available during the file transfer process if you have the right libraries installed and configured. They use a feature called the Alternative PHP Cache. When a PHP script receives an uploaded file, the interpreter will automatically check the $_POST array for a hidden field named APC_UPLOAD_PROGRESS, which becomes a cached variable, storing information about the upload so your scripts can access it. With this information cached and at your fingertips, you can give your users visual feedback to improve their user experience.

We will cover the implementation of the APC code in your HTML form, as well as identifying it in your PHP and how to access the cached information. There are many ways to represent this data — from Ajax to FLEX — but what we will focus on is how to prepare access to the data these front-end technologies will need.





Setting up

APC is not enabled by default in PHP V5.2. Since the new hooks are a part of APC, we need to make sure to install the extension and make it available to the PHP interpreter. This is accomplished by downloading the php_apc extension files. In our case, we are using an installation of WAMP, a freely available packaged PHP for Windows®, which includes Apache and MySQL. It offers a nice user interface and is easy to manage with menus that support configuration options.

To set up APC on WAMP:

  1. See Resources to download the libraries and WAMP.
  2. Install WAMP.
  3. Put the php_apc.dll file in the extensions folder for PHP. This is /php/ext by default.
  4. Use the system tray WAMP menu to select PHP settings>PHP Extensions>Add Extension.
  5. In the command-line interface that pops up, type php_apc.dll and press Enter.
  6. Using a text editor, open /php/php.ini and add the line apc.rfc1867 = on (it doesn't matter where). If you're trying to test locally and plan to upload large files so you can actually see progress, you'll also want to add the following directives: apc.max_file_size = 200M, upload_max_filesize = 200M, and post_max_size = 200M. Don't do this on a live production server, though, or you're likely to use up bandwidth and disk space allotments, not to mention slowing everyone else down to a crawl.
  7. Restart PHP.

APC should now be set up and initialized. The RFC1867 features of APC — the features that enable you to track file uploads — should now be enabled as an option, and you should be ready to look into our file uploads to enable real-time status.

No comments: