PHP - Uploading files

October 2016

The PHP language allows managing files upload through HTML form.

Form for sending files

The first step is to create an html form which will allow the user to open a dialog box for selecting the file to send:

<FORM method="POST" action="[your file PHP for upload.php]" ENCTYPE="multipart/form-data"> 
          <INPUT type=hidden name=MAX_FILE_SIZE  VALUE=2048> 
          <INPUT type=file name="name_of_file"> 
          <INPUT type=submit value="send"> 

You should not forget the attribute ENCTYPE="multipart/form-data" which is however very important to disclose the form.

The field MAX_FILE_SIZE is an indication of the maximum size to be uploaded by the browser. However this is not sufficient to ensure the maximum size of uploaded files. The value of the maximum size of the uploaded file is variable in the configuration file php.ini.

PHP Configuration to enable upload

The file configuration php.ini contain the guidelines that allows or not the sending of files through a form
  • file_uploads= On/Off determine files upload.
  • upload_tmp_dir = directory sets the temporary directory to host the uploaded file.
  • upload_max_filesize= 2M determines the maximum size allowed for the file. If the file exceed the limit, the server will send an error code.
  • post_max_size =indicates the maximum data size of a form. This directive takes precedence over *upload_max_filesize, it must be ensured to have more than post_max_size upload_max_filesize

If you cannot access the configuration (for example: site hosted on the server of the ISP or a shared host), you can check the configuration through phpinfo.


File recovery with PHP

The file as well as the information can be accessed through the variant superglobale $_FILES[].

To view the content, you can use the following script:
<pre><?  print_r($_FILES); ?></pre>

The code will be as follows :

    [name_of_file] => Array 
            [name] => YourImage.jpg 
            [type] => image/jpg 
            [tmp_name] => complete_path_of_uploaded_file 
            [error] => 0 
            [size] => 1000 


The above is JPEG image of 1mb size.

The fields $_FILES[name], $_FILES[type], $_FILES|error], $_FILES[size] allows to perform assessement of type of file, size, name verify errors.

You can thus examine errors as follows:
if ($_FILES['nom_du_fichier']['error']) { 
          switch ($_FILES['name_of_file']['error']){ 
                   case 1: // UPLOAD_ERR_INI_SIZE 
                   echo"The file exceed  the limit allowed by the server ] (file php.ini) !"; 
                   case 2: // UPLOAD_ERR_FORM_SIZE 
                   echo " The file exceeds the limit allowed in the HTML form!"); 
                   case 3: // UPLOAD_ERR_PARTIAL 
                   echo " Sending the file has been interrupted during transfer 
                   case 4: // UPLOAD_ERR_NO_FILE 
                   echo " The file you sent has zero size 
else { 
 // $_FILES['name_of_fle']['error'] value 0 or UPLOAD_ERR_OK 
 // no error 

The function move_uploaded_files() enable image transfer from temporary directory to destination directory


if ((isset($_FILES['name_of_file']['file'])&&($_FILES['name_of_file']['error'] == UPLOAD_ERR_OK)) { 
$path_destination = '/var/www/files/'; 
move_uploaded_file($_FILES['name_of_files']['tmp_name'], $path_destination.$_FILES['name_of_file']['name']); 

Related :

This document entitled « PHP - Uploading files » from CCM ( is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.