PhpRiot
Download Article
Download this article in PDF format with all listings and files.

Price: $5.00 AUD
(Approx. $4.35 USD)

More information
Related Books
Learning PHP, MySQL, and JavaScript: A Step-By-Step Guide to Creating Dynamic Websites (Animal Guide)

Learning PHP, MySQL, and JavaScript: A Step-By-Step Guide to Creating Dynamic Websites (Animal Guide)

If you know HTML, this guide will have you building interactive websites quickly. You'll learn...

The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP (Essentials)

The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP (Essentials)

Dreamweaver CS4 is a massive step forward in terms of integration with the rest of the CS4 suite...
PhpRiot Newsletter
Your Email Address:

More information

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)
<?php
    require_once('FileUploader.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