PHP File uploads
PHP provides file uploading in-built functionality, there are you can upload both text & binary files. It provides full control over on during file uploading i.e:
-
who is allowed to upload file.
-
what is to be done with the file once it has been uploaded.
PHP file uploading standard
-
PHP is capable of receiving file uploads from any RFC-1867 compliant browser.
-
PHP also supports PUT-method file uploads as used by Netscape Composer and W3C's Amaya clients.
Runtime configuration
Name | Default | Changeable | Changelog |
---|---|---|---|
file_uploads | "1" | PHP_INI_SYSTEM | |
upload_tmp_dir | NULL | PHP_INI_SYSTEM | |
max_input_nesting_level | 64 | PHP_INI_PERDIR | Available since PHP 5.3.9. |
max_input_vars | 1000 | PHP_INI_PERDIR | Available since PHP 5.3.9. |
upload_max_filesize | "2M" | PHP_INI_PERDIR | |
max_file_uploads | 20 | PHP_INI_SYSTEM | Available since PHP 5.2.12. |
Predefined constants
-
UPLOAD_ERR_OK – Value: 0; There is no error, the file uploaded with success.
-
UPLOAD_ERR_INI_SIZE – Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
-
UPLOAD_ERR_FORM_SIZE – Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
-
UPLOAD_ERR_PARTIAL – Value: 3; The uploaded file was only partially uploaded.
-
UPLOAD_ERR_NO_FILE – Value: 4; No file was uploaded.
-
UPLOAD_ERR_NO_TMP_DIR – Value: 6; Missing a temporary folder. Introduced in PHP 5.0.3.
-
UPLOAD_ERR_CANT_WRITE – Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.
-
UPLOAD_ERR_EXTENSION – Value: 8; A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help. Introduced in PHP 5.2.0.
How to upload a file in few step
Step-1 : create a html form named 'form.php'.
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" name="upload" value="upload" />
</form>
Step-2 : create a file name 'upload.php'.
<?php
$upload_dir = '/var/www/html/upload';
$upload_file = $upload_dir .basename( $_FILES['file']['name'] );
if( move_uploaded_file( $_FILES['file']['tmp_name'], $upload_file ) )
{
echo "File successfully uploaded.";
}
else
{
echo "File did not uploaded!";
}
?>
Output : if move_uploaded_file() function successfully run, it will display success message otherwise display error message as per given code.
How to catch success and error message during file upload
<?php
print_r($_FILES);
?>
It will display data in array form:
Array
(
[file] => Array
(
[name] => Sample_image.png
[type] => image/png
[tmp_name] => /opt/lampp/temp/phpSO1SSy
[error] => 0
[size] => 159202
)
)
in this array [error] key data return either 0 or 1.
-
0 - means file successfully uploaded.
-
1 – means file did not uploaded.
You can get all error exception messages related to file uploading via file uploading predefined constants, these are given above here:
How to upload multiple files at once
Step-1 : create a html form named 'form.php'.
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file[]" />
<input type="file" name="file[]" />
<input type="file" name="file[]" />
<input type="file" name="file[]" />
<input type="submit" name="upload" value="upload" />
</form>
Step-2 : create a file name 'upload.php'.
<?php
foreach ($_FILES["file"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["file"]["tmp_name"][$key];
$upload_dir = '/var/www/html/upload/';
$name = basename($_FILES["file"]["name"][$key]);
move_uploaded_file( $tmp_name, $upload_dir"/".$name );
}
}
?>