Upload any file using a PHP Script

This PHP upload class can be used to build scripts and applications with the feature to upload files to a web server. The PHP script has features like: file-extension check, renaming files, file name validation, maximum file- size limitation, MIME type detection and a flexible error reporting system. The class is extremely easy to use and comes together with full working examples. Since version 2.x the is a class extension that makes it easier for the developer to upload multiple files. Check the example files and class file for all update information. Since version 2.22 the class is written to handle requests if the PHP directive "register_globals=off". Since version 2.25 there is a class extension for photo uploads. This extension is upload photos (images) and resize them to a given maximum width and height. Since version 2.26 there is support for the ImageMagick command line tools. This feature is an option and works faster then PHP functions from the GD library.

Visit here Script Demo - PHP Script Download

Documentation and support

All update information - Post here your questions and suggestions.

Related forum posts

Variables (upload_class.php)

$the_file

The name of the original (uploaded) file

$the_temp_file

The temporay name of the uploaded file

$validate_mime

A switch do enable/disable MIME type detection

$upload_dir

The directory where the file will be uploaded. A non existing directory will be created. Currently the is support for a new directory one level above the existing directory!

$replace

Set this var to true if an existing file should be replaced (default = false)

$do_filename_check

This var is used as a switch, possible setting y/n. In position 'y' the name is validated while using regular expressions.

$max_length_filename

Set this value to take care of the maximum length of a filename (default = 100)

$extensions

This array holds all the file extensions allowed for an upload, you can add so much as you want.

$valid_mime_types

An array with most common MIME types. Used to validate a file's MIME type.

$ext_string

This var is used for output the allowed file extensions.

$language

You need this var if your messages should be in an other language then English

$http_error

This variable is required, it holds the error reported by $_FILES array.

$rename_file

Set this boolean if you want to (auto)rename a file (I use a timestamp as a random filename)

$file_copy

This var holds the new name after upload (note this name might be different from the uploaded file if you rename/copy the file)
Use this variable always if you save the filename in a database or in a variable!

$message

This array acts as a container for all messages generated during the upload process.

$create_directory

Switch this boolean to create the upload directory if it doesn't exist (one level up, read also the information about the variable >$upload_dir)

Methodes (upload_class.php)

file_upload()

The constructor, sets the language, the rename boolean and empties the file extension string.

show_error_string($br = '<br />')

This method is used to show all strings inside the message array

set_file_name($new_name = '')

Checks if a custom filename exists, sets a default (new) filename or keeps the original one

upload($to_name = '')

This method is used to handle the upload process, $to_name is an additional custom name parameter (works together with the rename boolean)

check_file_name($the_name)

Checks the "spelling" of a filename, the length and if the name value is not empty.

get_extension($from_file)

Isolate the files extension with this method

validateMimeType($mime_type)

Checks if the current MIME type is equal to the type from the extension

validateExtension()

This method checks for a valid file extension

show_extensions()

Use this method to build the "allowed" extensions string

move_upload($tmp_file, $new_file)

This method is a group of functions which move the temporary file to his propper upload direction.

check_dir($directory)

Check if the upload directory exists or not and if the boolean "$create_directory" is true the directory will be created.

existing_file($file_name)

This method checks if a file already exists

get_uploaded_file_info($name)

Simple function to show file information like: file size, file name, MIME type and image dimensions (if available)

get_mime_type($file)

Get the MIME type for a file. The function is using the Fileinfo extension or mime_content_type() for older systems.

del_temp_file($file)

A flexible function to delete an uploaded file.

create_file_field($element, $label = '', $length = 25, $show_replace = true, $replace_label = 'Replace old file?', $file_path = '', $file_name = '', $show_alternate = false, $alt_length = 30, $alt_btn_label = 'Delete image')

A dynamic input/file form field with a switch to show a file or the form field.

error_text($err_num)

This method is a multilanguage error selector, the property holds the error number