Home / code / php

File upload with PHP

PHP is capable of receiving file uploads from any RFC-1867 compliant browser (which includes Netscape Navigator 3 or later, Microsoft Internet Explorer 3 with a patch from Microsoft, or later without a patch). This feature lets people upload both text and binary files. With PHP's authentication and file manipulation functions, you have full control over who is allowed to upload and what is to be done with the file once it has been uploaded.

Note that PHP also supports PUT-method file uploads as used by Netscape Composer and W3C's Amaya clients.

A file upload screen can be built by creating a special form which looks something like this:

<form enctype="multipart/form-data" action="_URL_" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000">
Send this file: <input name="userfile" type="file">
<input type="submit" value="Send File">
</form>
The _URL_ should point to a PHP file. The MAX_FILE_SIZE hidden field must precede the file input field and its value is the maximum filesize accepted. The value is in bytes.

Variables defined for uploaded files differs depends on PHP version and configuration. Following variables will be defined within the destination script upon a successful upload. When track_vars is enabled, $HTTP_POST_FILES/$_FILES array is initialized. Finally, related variables may be initialized as globals when register_globals is turned on. However, use of globals is not recommended anymore.

$HTTP_POST_FILES/$_FILES is provided to contain the uploaded file information.

The contents of $_FILES are as follows. Note that this assumes the use of the file upload name 'userfile', as used in the example script above:

$_FILES['userfile']['name']
The original name of the file on the client machine.

$_FILES['userfile']['type']
The mime type of the file, if the browser provided this information. An example would be "image/gif".

$_FILES['userfile']['size']
The size, in bytes, of the uploaded file.

$_FILES['userfile']['tmp_name']
The temporary filename of the file in which the uploaded file was stored on the server.


When register_globals is turned on in php.ini the available variables are as follows. Note that the following variable names assume the use of the file upload name 'userfile', as used in the example script above:

  • $userfile - The temporary filename in which the uploaded file was stored on the server machine.
  • $userfile_name - The original name or path of the file on the sender's system.
  • $userfile_size - The size of the uploaded file in bytes.
  • $userfile_type - The mime type of the file if the browser provided this information. An example would be "image/gif".

Files will by default be stored in the server's default temporary directory, unless another location has been given with the upload_tmp_dir directive in php.ini. The server's default directory can be changed by setting the environment variable TMPDIR in the environment in which PHP runs. Setting it using putenv() from within a PHP script will not work. This environment variable can also be used to make sure that other operations are working on uploaded files, as well.

 

Validating file uploads

A example how to validate a uploaded html file smaller then 100Kb.

if($thefile_type != 'text/html' || $thefile_size > (1024*100) || ! eregi( '.html?$', $thefile_name ))
{
  $error .= ("uploaded file type refused ($thefile,$thefile_name,$thefile_type)");
}
else
{
  $result = move_uploaded_file( $thefile, $upload_install_dir . $thefile_name );
}

 

TOP

Latest script:

 

Books: