Uploading files to a server is easy, but that comes with the risk of being destroyed, so be careful when allowing files to be uploaded.
Set up the "php.ini" file
Search for the file_uploads directive in the "php.ini" file and set it to On:
file_uploads = On
Now,
Create an HTML form: allow users to select the files they want to upload
For HTML upload forms, make sure to use method="post" and enctype="multipart/form-data". Because without these, file upload will not work.
<!DOCTYPE html><html><body><form action="fileToupload.php" method="post" enctype="multipart/form-data">
Select an Image To Upload:-<input type="file" name="fileToUpload" id="fileToUpload"><input type="submit" value="Upload Image" name="submit"></form></body></html>
Output:
Create a PHP script to upload files
<?php$target_dir="uploads/";$target_file=$target_dir.basename($_FILES["fileToUpload"]["name"]);$uploadOk=1;$imageFileType=pathinfo($target_file,PATHINFO_EXTENSION);// Check if image file is a actual image or fake imageif(isset($_POST["submit"])){
$check=getimagesize($_FILES["fileToUpload"]["tmp_name"]);if($check!==false){
echo"File is an image - ".$check["mime"].".";$uploadOk=1;}else{
echo"File is not an image.";$uploadOk=0;}}?>
In the above code,
"$target_file" defines the file path to be uploaded
"$uploadOk=1" will be used later.
“$target_dir = uploads/” – defines the directory where files are to be placed
"$imageFileType" The file extension of the saved file.
Check if the file already exists
First, let's check if the file already exists in the "uploads" folder. If present, display an error message and set $uploadOk to 0:
//To check if file already exists or notif(file_exists($target_file)){
echo"Sorry, the file already exists.";$uploadOk=0;}
Limit file sizes and types
Now check if the file size is greater than 500kb; an error message will be displayed and $uploadOk is set to 0.
// To check file sizeif($_FILES["fileToUpload"]["size"]>500000){
echo"Sorry, your file is too large.";$uploadOk=0;}
The given code allows users to upload only JPEG, JPG, GIF and PNG files. If you upload other file types, an error message is displayed until $uploadOk is set to 0:
// To allow certain file formatsif($imageFileType!="jpg"&&$imageFileType!="png"&&$imageFileType!="jpeg"&&$imageFileType!="gif"){
echo"Sorry, only JPEG, JPG, GIF & PNG files are allowed.";$uploadOk=0;}
Finally, combine all the above codes. Here is the complete populated upload PHP script:
<?php$target_dir="uploads/";$target_file=$target_dir.basename($_FILES["fileToUpload"]["name"]);$uploadOk=1;$imageFileType=pathinfo($target_file,PATHINFO_EXTENSION);// To check if image file is actual or fake imageif(isset($_POST["submit"])){
$check=getimagesize($_FILES["fileToUpload"]["tmp_name"]);if($check!==false){
echo"File is an image - ".$check["mime"].".";$uploadOk=1;}else{
echo"File is not an image.";$uploadOk=0;}}// To check if file already existsif(file_exists($target_file)){
echo"Sorry, the file already exists.";$uploadOk=0;}//To check the file sizeif($_FILES["fileToUpload"]["size"]>500000){
echo"Sorry, your file is too large.";$uploadOk=0;}// To allow certain file formatsif($imageFileType!="jpg"&&$imageFileType!="png"&&$imageFileType!="jpeg"&&$imageFileType!="gif"){
echo"Sorry, only JPEG, JPG, GIF & PNG files are allowed.";$uploadOk=0;}// To check if $uploadOk is set to 0 by an errorif($uploadOk==0){
echo"Sorry, your file was not uploaded.";}else{
if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_file)){
echo"The file ".basename($_FILES["fileToUpload"]["name"])." has been uploaded.";}else{
echo"Sorry, there was an error uploading your file.";}}?>