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

Retrieving a File's Upload Status

Next we write another PHP script, which is used to send back the status of a file as it is being uploaded. Essentially this is an interface to the getUploadStatus() method of the FileUploader PHP class.

This script will be utilized by the Ajax code we will develop later in this chapter.

As we saw earlier in this article, the getUploadStatus() method returns an array containing the upload data for the file with the given ID. We are going to send this data back to the browser using JSON (JavaScript Object Notation).

Since PHP 5.2.0, data can easily be encoded into JSON format using the json_encode() method. The HTTP Content-type header associated with JSON data is application/json.

Listing 13 Sending the status of a file as it is being uploaded (status.php)
    $id = isset($_POST['id']) ? $_POST['id'] : 0;
    $fu = new FileUploader();
    $status = $fu->getUploadStatus($id);
    header('Content-type: application/json');
    echo json_encode($status);

In This Article

Additional Files