0

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" ?

2
Contributors
1
Reply
17
Views
4 Years
Discussion Span
Last Post by 203428
0

so;
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">
</form>

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

<?php
 $dest_dir=$dir="path/to/your/storing/dire";
    if($_FILE["name"]!=null){
            list($w,$h)=getimagesize($_FILE["tmp_name"]);
            $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);
               }
            $nw=750;
            $nh=230;

            $tmp=imagecreatetruecolor($nw,$nh);
            imagecopyresampled($tmp,$src,0,0,0,0,$nw,$nh,$w,$h);
            $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
            imagedestroy($src);
            imagedestroy($tmp);


            }

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')      
  VALUES('"+$name+"','"+$path+"')";
  mysql_query($sql,$con);
  mysql_close($con);

}         
?>

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

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

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

Edited by 203428

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.