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



Introduction

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:


<?php
  $upload
= 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:


<?php
  $upload
= 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()) {
    echo
'File ' . $upload->filename . ' has been uploaded successfully.';
  } else {
    echo
'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.

parse()

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.

save()

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

set_file($file)

Sets the file property to $file.

set_destination($destination)

Sets the destination property to $destination.

set_permissions($permissions)

Sets the permisions property to $permissions.

set_filename($filename)

Sets the filename property to $filename.

set_tmp_filename($filename)

Sets the tmp_filename property to $filename.

set_extensions($extensions)

Sets the extensions property to $extensions.

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

check_destination()

Checks if the destination exists and is writable.

Class Properties



$file

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)

$filename

This property contains the filename of the uploaded file.

$destination

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

$permissions

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

$extensions

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

$tmp_filename

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.