My problem is

"How to upload the image, while uploading store the filename with the extension in the database and while retrieving the image filename should be taken from the database and file should be taken from corresponding folder" ?

1)you must creae a data base call him DBtest with table call him table_image.
2) add some columns to your table table_image lets said :

image_id: int, NN, PK
image_name: Sting    ex ::=> (image.png)
image_path: String

3) create form with file uploader tag ex::=> <input tyle="file" name="image" > and enctype="multipart/form-data" in form tag see exemple below:

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" ><br>
<input type="submit" name="submit" value="Submit">

4) Stroring operation --> in upload_file.php file :

            $filename = stripslashes($_FILE['name']);
            $extension = getExtension($filename);
            $extension = strtolower($extension);
            if($extension=="jpg" || $extension=="jpeg" )
                 $uploadedfile = $_FILE['tmp_name'];
                 $src = imagecreatefromjpeg($uploadedfile);
            else if($extension=="png")
               $uploadedfile = $_FILE['tmp_name'];
               $src = imagecreatefrompng($uploadedfile);

            $filename = $dir. $_FILE['name'];// here we concat path and filename
            imagejpeg($tmp,$filename,100); // store in directory
            StorImageInDB($dir,$_FILE['name']); // store in Data base
            // destroy all temp variable


function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; } 
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
Function StorImageInDB($name,$path){

    $con = mysql_connect("url","mysql_user","mysql_pwd")or die('Could not connect: ' . mysql_error());
  $sql = "INSERT INTO DBtest.table_image('image_name','image_paht')      


5) Retrieving operation --> lets call this file Show_image.php

   $con = mysql_connect("url","mysql_user","mysql_pwd")or die('Could not connect: ' . mysql_error());
   $sql = "SELECT * FROM DBtest.table_image";

while($row = mysqli_fetch_array($result)){

 echo "<img src=".$row['image_path']."/".$row['image_name']." width='100px' heigh='100px' alt=".$row['image_name'].">";



hop this help
Best ragards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.