Secure Image Uploading on Server Using PHP

25-09-2017

Secure Image Uploading on Server Using PHP

Upload the Image file on the server using a secure way. While uploading images on the server, the Developer must take care of certain security so that attackers would not upload any infected miscellaneous files that contain the virus.


In PHP, File uploading is the most sensitive part when it comes to security. When a developer allows a user to upload a file, validation must be done in such a way that only allowed file types should be uploaded to the server. If validation is not done in the right way, an attacker can upload any type of file on the server and it will ruin your web application. For example, an attacker may upload a PHP file that can display each file and its contents on the server. An attacker can write a code in the PHP file to view all user's credentials and can log in to any user account, OR can Delete the entire database, anything can happen.

Security Key Points of Validation While Uploading File

Here, i'm listing the Security Key Points of Validation While Uploading File.

Imgae Uploading Code Demo With Sample Code Download


	<form action="" method="post" enctype="multipart/form-data">
	<input type="file" name="image" required>
	<input type="submit" name="upload" value="Upload File">
	</form>
	<?php
	if( isset($_POST["upload"]) ){
	//if form is submitted

	// allowed extension array
	$allowedExts = array("jpg", "jpeg", "gif", "png");
	//Temprory name of file
	$tmp_name_of_file = explode(".", $_FILES["image"]["name"]);
	//Extension of uploaded file
	$extension = end($tmp_name_of_file);
	//Checking uploaded file type
	//if it is jpeg image, it will print 'image/jpeg'
	$file_type = $_FILES["image"]["type"];
	// Checking file type and file extension
	if( $file_type == "image/gif" || $file_type == "image/jpg" || $file_type == "image/jpeg" || $file_type == "image/png" && in_array($extension, $allowedExts))
	{
	  // file type and extension is OK

	   // checking if any error in uploaded file
	   if ($_FILES["image"]["error"] > 0) {
	     echo "Error: " . $_FILES["image"]["error"];
	     exit(0);
	  }else{
	  	// No error in uploaded file, we can move next
	  	/// check mime file content
	  	$verifyimg = getimagesize($_FILES['image']['tmp_name']);
	   // $verifyimg is an array containing image width , height, mime etc..
	   //print_r($verifyimg);
	  	if(  $verifyimg['mime'] != 'image/png' &&  $verifyimg['mime'] != 'image/jpg' &&  $verifyimg['mime'] != 'image/jpeg' &&  $verifyimg['mime'] != 'image/gif'  ) {
	  	  echo "Only  images are allowed!";
	  	  exit(0);
	  	 }else{
	  	 	// image mime checked
	  	 	// image file type and extension checked
	  	 	//now check using getimagesize
	  	 	 $image_info = getimagesize($_FILES['image']['tmp_name']); ///////==========
	  	 	if($image_info == false) {
	  	 	   echo "Not a valid image!";
	  	 	   exit(0);
	  	 	 }else{
	  	 	 	// image is also valid
	  	 	 	//now, check size
	  	 	 	$allowed_size = 100000; //it is in byte// 100000 byte = 100KB
	  	 	 	if($_FILES['image']['size'] > $allowed_size){
	              echo "Allowed size is 100KB ";
	              exit();
	  	 	 	}else{
	              //Ready to uploaded
	  	 	 		$image = $_FILES["image"]["name"];
	  	 	 		$image  = time().rand(1,888).$image;
	  	 	 		// $image variable is file name, you can store this name in database
	  	 	 		$tmp_name = $_FILES["image"]["tmp_name"];
	  	 	 		move_uploaded_file($tmp_name, "uploads/".$image); ////storage 
	  	 	 		echo "File Uploaded";

	  	 	 	}

	  	 	 }

	  	 } 

	  }

	}else{
	// Invalid File Type and Extension
	echo "Invalid image type";
	exit(0);
	}




	} //form submition if end
	?>



Download Sample Code


Latest Published Blog


PurgeCSS - Remove unused CSS from Web Pages

PurgeCSS - Remove unused CSS from Web Pages

03-04-2023

It is generally a good practice to remove unused CSS from web pages. As mentioned earlier, removing unused CSS can provide several benefits, including faster page loading times, improved website performance, reduced page weight, and better code maintainability.


Installing Asterisk16 on CentOs7

Installing Asterisk16 on CentOs7

14-04-2022

Here is Step-by-Step Guide to Installing Asterisk16 on CentOs7.


Make CentOS7 Full Screen Like Primary Operating System in VirtualBox

Make CentOS7 Full Screen Like Primary Operating System in VirtualBox

01-03-2022

Learn How to Make CentOS7 Full Screen Like Primary Operating System in VirtualBox. After Installing CentOS in VirtualBox, CentOs Screen does not Occupy the Full Window Screen. Here in this Tutorial, We Will See how to do it.


Setting  Key to a Column For Quick Fetch Operation From Larze Records

Setting Key to a Column For Quick Fetch Operation From Larze Records

14-02-2019

To set a key to a column in SQL, you need to create an index on that column. An index is a data structure that allows the database to quickly look up records based on the values in the indexed column. By creating an index on a column, you can improve the performance of queries that filter, sort, or group by that column.