PHP Classes

PHP Uploader class: Generate upload forms and handle uploaded files

Recommend this page to a friend!
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 257 All time: 7,798 This week: 263Up
Version License PHP version Categories
uploader-class 1.0The PHP License5HTTP, PHP 5, Files and Folders, AJAX
Description Author

This class can generate upload forms and handle uploaded files.

It can generate HTML and JavaScript for a form to upload a single file using AJAX.

The class can also handle the file upload AJAX requests and validates the files, sending them to a given directory.

Picture of Rizza
  Performance   Level  
Name: Rizza <contact>
Classes: 2 packages by
Country: United Kingdom
Age: ???
All time rank: 3773161 in United Kingdom
Week rank: 587 Up21 in United Kingdom Up



The uploader-class attempts to greatly reduce the amount of code required to make uploading files possible in php.

Dependencies include:

- jQuerey - Access to session functionality. (including the use of cookies)

The code required to produce an upload form is as follows:

<script src=""></script>
<link rel="stylesheet" type="text/css" href=""/>

//First we require our uploader class
//Then we create a new instance of the uploader class
$upload=new \uploader\upload(__DIR__."/uploads/",array("image/png","image/jpeg"),"test1","next.php");
//finally we get the html for that segment
echo $upload->get_html_segment();

When the file is uploaded to the server, using ajax, an endpoint has to be available so the file can be validated. It is recommended this php file is not hidden behind any .htaccess rules. The example code for this file is as follows:

//Headers cannot be sent before this point
//Simply call the handle upload function and any uploads will be automatically verified (The function will exit on it being called).

The uploader-class

A class to allow for upload of files to specified locations on the given server. Should handle all client and sever side interaction during upload of files via AJAX.This includes:

1. Generation of HTML for client. 2. The handling of JavaScript to send the file to the server. 3. To store session variables so uploads can be accepted and/or validated before being placed at the given directory.


  • string \$upload_dir The directory to upload files to.
  • int \$upload_id A unique identifier for the class instance.
  • int \$upload_limit The maximum sized files can be uploaded in (in bytes.)
  • string[] \$accepted_datatypes The list of mime types that are acceptable for
  • string \$file_name The name of the file after it has been uploaded (File extention should not be given.)
  • string \$upload_endpoint where the form should point to for file uploads to be allowed.
__construct ( \$dir\_to\_upload\_to, \$accepted\_datatypes, \$file\_name, \$upload\_endpoint, \$max\_size = 52428800 )

string$dir_to_upload_to* gives a specified upload directory string[]$accepted_datatypesA list of the accepted datatypes (must be in mime format without "" wildcards) string$file_name* The file name that should be held as the name of the file upon it being uploaded string$upload_endpoint* The endpoint the upload handle should try to upload information to. number$max_size* maximum accepted size of file uploaded(in bytes)

Things of note:

1. File uploaded automatically overwrite other files. This is true of all defined mime types. 2. Mime-types need to be used carefully , the "*" wildcard cannot be used in any capacity. Only direct mime types can be given. 3. The php_fileinfo extention needs to be enabled.

  Files folder image Files  
File Role Description
Files folder imagedocs (1 file)
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file next.php Aux. Auxiliary script
Accessible without login Plain text file Doc. Documentation
Plain text file uploader.php Class Class source

  Files folder image Files  /  docs  
File Role Description
  Accessible without login Plain text file Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:7,798
This week:263Up