PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Monitoring File Uploads using Ajax and PHP

Using APC to Monitor a File Upload

Assuming you have installed APC and enabled the apc.rfc1867 setting (as described in the previous section), the APC upload monitoring will be automatically activated when a file upload occurs which also contains a form value called APC_UPLOAD_PROGRESS. The value of this field is a value of your own choosing that you can use to identify the upload.

For example, the following form could be used to activate upload monitoring:

Listing 5 A sample form used to activate APC upload monitoring (listing-5.html)
<form method="post" action="upload.php" enctype="multipart/form-data">
    <div>
        <input type="hidden" name="APC_UPLOAD_PROGRESS" value="1234" />
        <input type="file" name="myFile" />
        <input type="submit" value="Upload file..." />
    </div>
</form>
Note: The APC_UPLOAD_PROGRESS form element must appear in the form before the file input.

When this form is submitted, the file upload begins as usual. APC can now be polled to monitor the upload status (this must be done in a separate request, since the primary request is being used to fulfil the upload – we'll deal with this later using Ajax).

To retrieve cache data from APC, the apc_fetch() function is used. We use the value of the APC_UPLOAD_PROGRESS form element when calling this function. In the above example, the value of the field is 1234. We must prefix this value with upload_, meaning in order to fetch the upload information about upload 1234 we use apc_fetch('upload_1234').

If no cache data is found for the key passed to apc_fetch() then false is returned. On the other hand, if data is found (as it should be if everything is configured correctly), an array will be returned. This return array contains the following useful values:

  • done: A Boolean value indicating whether or not the upload has completed.
  • total: The total file size in bytes of the file being uploaded.
  • current: The number of bytes that have been completed.

These values allow us to determine how much of the upload has completed, as demonstrated by the following code.

Listing 6 Retrieving Upload Data from APC (listing-6.php)
<?php
    $upload = apc_fetch('upload_1234');
    
    if ($upload) {
        if ($upload['done'])
            $percent = 100;
        else if ($upload['total'] == 0)
            $percent = 0;
        else
            $percent = $upload['current'] / $upload['total'] * 100;
    }
?>

Hard-coding the ID value for the upload obviously is neither ideal nor a practical solution, so part of the solution we implement in the next section will use code similar to above in conjunction with PHP sessions. Additionally, we will also handle the case where APC isn't installed so your code will work across all servers.

In This Article


Additional Files