PHP - Uploading files
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:
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.
= On/Off determine files upload. upload_tmp_dir = directory sets the temporary directory to host the uploaded file.
= 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.
= 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.
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:
print_r($_FILES); ?>
The code will be as follows :
Array ( [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) !"; break; case 2: // UPLOAD_ERR_FORM_SIZE echo " The file exceeds the limit allowed in the HTML form!"); break; case 3: // UPLOAD_ERR_PARTIAL echo " Sending the file has been interrupted during transfer !"; break; case 4: // UPLOAD_ERR_NO_FILE echo " The file you sent has zero size !"); break; } } 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']); } ?>