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

Handling the File Upload

In the index.php file created in the previous section, the form is submitted to a file called upload.php. The upload.php file is simply used to make a call to the upload() method of the FileUploader PHP class.

The other thing we do in this file is specify the directory where the file should be saved. Typically your own upload functionality will behave slightly different, but for our purposes this will suffice.

Note: This directory must be writable by the web server.
Listing 12 Handling the file upload (upload.php)
<?php
    require_once('FileUploader.php');
 
    $path = realpath(dirname(__FILE__) . '/uploads');
 
    $fu = new FileUploader();
    $fu->upload('myFile', $path);
?>

The first argument passed to upload() is the name of the file form element in index.php, while the second argument is the path where the file should be saved.

In This Article


Additional Files