File Uploading ClassLast Update: 7th April, 2005
Article ID: 9


The self-contained file upload class allows uploaded files to be easily processed and stored securely on the server with just one line of code.

A basic example of using the class is as follows:

= new upload('file_form_name', '/www/server/uploads/', '777', 'zip');

The upload class will look in the $_FILES, $HTTP_POST_FILES, or $GLOBALS scope (depending on the PHP version used) for the file_form_name value and store the file in the /www/server/uploads/ directory with the permissions of 777 if the uploaded file has an extension of zip.

The file will automatically be processed when the first two arguments have been passed (the form file field name, and the destination directory).

An example of manually processing an uploaded file is as follows:

= new upload('file_form_name');
$upload->set_destination('/www/server/uploads/'); // mandatory
$upload->set_permissions('777'); // optional
$upload->set_extensions('zip'); // optional

if ($upload->parse() && $upload->save()) {
'File ' . $upload->filename . ' has been uploaded successfully.';
  } else {
'Error processing ' . $upload->filename . '.';

The file upload class automatically stores successfull or failure messages in the message stack class instance.

Class API

Class Methods

upload($file = '', $destination = '', $permissions = '777', $extensions = '')

Class constructor; creates an upload object.

If the $file and $destination arguments are provided, the constructor automatically parses (parse()) and saves (save()) the uploaded file.

If the parsing or saving fails, the object destructs itself automatically.


Looks through the $_FILES, $HTTP_POST_FILES, and $GLOBALS scope for the file to be processed. The file will exist in one of these scopes depending on the PHP version in use.

If the $extensions variable has been set, a check is made to see if the extension type of the uploaded file is allowed or not.

The destination directory and writing permission is also checked.


Moves the uploaded file to the destination directory and sets the assigned file permissions.


Sets the file property to $file.


Sets the destination property to $destination.


Sets the permisions property to $permissions.


Sets the filename property to $filename.


Sets the tmp_filename property to $filename.


Sets the extensions property to $extensions.

$extensions can either be a string containing one extension type, or an array containing many extension types.


Checks if the destination exists and is writable.

Class Properties


This property is set two times with different value types.

It is set in the constructor first as the form file field name of the uploaded file, and in the parse() method as an array containing the following elements:

name (file name)
type (file type)
size (file size)
tmp_name (temporary name on the server)


This property contains the filename of the uploaded file.


This property contains the destination to move the uploaded file to.


This property contains the permissions to set the uploaded file to.


This property is an array containing one or more allowable file upload types.


This property contains the temporary file name of the uploaded file on the server.



Trademark Policy | Copyright Policy | Sitemap

Copyright © 2000-2005 osCommerce. All rights reserved.