PHP (Core & Framework)

How to upload image to MySql database

In this tutorial we will learn how to upload an image in your data. 

To the previous created form add the following attribute: enctype=”multipart/form-data”. It specifies which content-type to use when submitting the form.

Without the requirements above, the file upload will not work.The enctype attribute lets you specify an encoding type for your form. 

Check if the file uploaded already exist or not

First, we will check if the file already exists in the “uploads” folder. If it does, an error message is displayed, and $uploadOk is set to 0.

Limit File Size

The file input field in our HTML form above is named “fileToUpload”.

Now, we want to check the size of the file. If the file is larger than 500KB, an error message is displayed, and $uploadOk is set to 0

Limit File Type

The code below only allows users to upload JPG, JPEG, PNG, and GIF files. All other file types gives an error message before setting $uploadOk to 0.

This is how your data will be displayed

PHP script explained:

  • $target_dir = “uploads/” – specifies the directory where the file is going to be placed
  • $target_file specifies the path of the file to be uploaded
  • $uploadOk=1  is not used yet (will be used later)
  • $imageFileType holds the file extension of the file (in lower case)
  • Next, check if the image file is an actual image or a fake image

Note:  You will need to create a new directory called “uploads” in the directory where “upload.php” file resides. The uploaded files will be saved there.



	$target_dir = "uploads/";
	$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
	$uploadOk = 1;
	$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;

if ($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.";

	$sql="insert into myrecords(user_id,type,title,startdate,enddate,postdate,file_name) values ('$uid','$t','$ti','$sd','$ed','$pd','$filename')";

		echo " Details Added Successfully !!!";
		echo "error:".$sql."<br>".mysqli_error($conn);
	<form method="post" name="myform" action="" enctype="multipart/form-data">
		Select Image:<input type="file" name="fileToUpload" id="fileToUpload"><br>
		Type:<select name="type">
			<option value=1>Insurance</option>
			<option value=2>FD</option>
		TITLE:<input type="text" name="title"><br>
		START DATE:<input type="Date" name="s"><br>
		END DATE:<input type="Date" name="e"><br>
		POST DATE:<input type="Date" name="p"><br>
		<input type="submit" name="submit" value="Add">


Posted By: Rhythm Arya

Comment here

5 + 3 =