How to Upload and retrieve image in php

15/04/2017   04:12:07 PM

Hello, in this tutorial I will explain how to upload and show image using php.

Image Uploading is very easy there are two ways you can upload image either to the database or in folder and save path in database. In this tutorial we see how to upload image in folder and save its path in database.

1) Create Database Table

  • First create a table inside your database. I have created a simple table named upload_image as you can see in the below image.

2) Create Index.php Page

<form method="post" action="upload.php" enctype="multipart/form-data">

<input type="hidden" name="MAX_FILE_SIZE" value="1000000"/><input name="uploadedfilee" type="file" style="height:35px;" /><br><br>

<input type="submit" value="submit" name="submit">



3) Create upload.php Page

In $target_path you can set your folder name my folder name is image and i can set like $target_path = "image/"; 


$target_path = "image/";

$target_path = $target_path . basename( $_FILES['uploadedfilee']['name']);

if(move_uploaded_file($_FILES['uploadedfilee']['tmp_name'], $target_path))


$conn = new mysqli("localhost", "root", "", "sampledb");

$sql = "INSERT INTO upload_image(`path`) VALUES ('$target_path')";

if ($conn->query($sql) === TRUE) {

echo "<br><br>";

} else {

echo "Error: " . $sql . "<br>" . $conn->error;


$sql1 = "SELECT path FROM upload_image order by id desc limit 1";

$result1 = $conn->query($sql1);

if ($result1->num_rows > 0) {

// output data of each row

while($row = $result1->fetch_assoc()) {


echo "<img src='$path' height='280' width='320' />";








Support us by Sharing