Simple Ajax Uploader - PHP Class API Reference

For setup instructions and more examples, visit the Github project page. For the Simple Ajax Uploader Javascript API reference, go here.

Basic Usage Example


<?php
require('Uploader.php');

$uploader = new FileUpload('uploadFile');        
$result $uploader->handleUpload('/uploadDir/');

if (!
$result) {
  echo 
json_encode(array(
          
'success' => false,
          
'msg' => $uploader->getErrorMsg()
       ));    
} else {
    echo 
json_encode(array(
            
'success' => true,
            
'file' => $uploader->getFileName()
         ));
}  

Properties

uploadDir - (string) File upload directory (include trailing slash)

Example: $uploader->uploadDir = '/uploads/';

allowedExtensions - (array) List of permitted file extensions

Example: $uploader->allowedExtensions = array('jpg', 'jpeg', 'png', 'gif');

sizeLimit - (integer) Max file upload size in bytes (default 10MB)

Example: $uploader->sizeLimit = 10485760;

newFileName - (string) Optionally save uploaded files with a new name by setting this

Example: $uploader->newFileName = 'newFile.jpg';

Methods

handleUpload( [ string $uploadDir ] [, array $allowedExtensions ] ) - Save the uploaded file.

getFileName() - Returns file name of the uploaded file.

getFileSize() - Returns file size of the uploaded file.

getExtension() - Returns file extension of the uploaded file.

getErrorMsg() - Returns error message, if any.

getSavedFile() - Returns file name, with path, of the newly saved file.

getMimeType( string $filename ) - Returns the MIME Content-type of a file. Uses the PHP finfo class for greater reliability than HTTP request data. Note: Requires reading the entire file into memory with file_get_contents.

isWebImage( string $filename ) - Returns true if a file is either a GIF, PNG, or JPEG, otherwise returns false. Uses exif_imagetype for speed and reliabiity.