PHP File Upload
Any file from client machine or user computer can be easily uploaded to server with PHP.
But the danger easily arrives, therefore be careful always when uploading files!
Configure The "php.ini" File
First, make sure that the file uploads are allowed with a PHP setting.
Find the file uploads directive in your "php.ini" file and enable it :
file_uploads = On
Create The HTML Form
Next, construct an HTML form that allows customers to select the picture file they want to upload :
For the HTML form above, there are some guidelines to follow :
- Check that the form's method="post" is used.
- The following characteristic is also required, enctype="multipart/form-data" It determines the type of material to utilize when you submit the form.
The file upload will not work without the required conditions.
The form above transfers data to a file called "upload.php", which we will construct next."
Create The Upload File PHP Script
The code for uploading a file can be found in the "upload.php" file.
Example :-
PHP script explained :-
- $target dir= "uploads/" - defines the destination directory (where to store all uploaded files) for the file.
- The path to the file to be uploaded is specified by $target_file.
- $uploadOk=1 has yet to be utilised (will be used later).
- $imageFileType contains the file extension (in lower case).
- Then examine whether the image file is a picture or a false picture.
Note :- In the directory containing the "upload.php" file, you must create a new directory called "uploads". The uploaded files will be kept there.
Related Links
Check if File Already Exists
We can now add some constraints.
First, we will check whether the file exists previously in the folder "uploads". If so, an error notice and $UploadOk
is set to 0 are displayed.
Example :-
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
Limit File Size
In our HTML form above, the file input area is labeled "fileToUpload".
Now we're going to look at the file's size. An error notice is presented if the file is larger than 500KB, and $uploadOk
is set to 0 :
Example :
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
Related Links
Limit File Type
Users can only upload JPG, JPEG, PNG, and GIF files using the code below. Before setting $uploadOk to 0 for all other file kinds, an error notice appears :
Example :-
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
Complete Upload File PHP Script
This is how the entire "upload.php" file now looks.
500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>