How to store the images in mysql table and retrieval using php?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deepucando
    New Member
    • Aug 2010
    • 2

    #1

    How to store the images in mysql table and retrieval using php?

    Hi

    i need help that how to store the images in the mysql table and retrieval of stored image using php code...

    sample code that helps me a lot..

    regards
    ~Deepu~
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    this should get you started

    Comment

    • ciranjeebxtreme
      New Member
      • Sep 2010
      • 4

      #3
      Hi deepu,

      For Image Entry And Retrieval You Need Two PHP Scripts...
      1.addimage.php-To Add Image Details In Database And the image in Webserver.
      2.showimage.php-To view Image Retrieving Data From Database..

      1.addimage.php

      Code:
      <html>
      <head></head>
      <body>
      
      <?php
      //define uploadpath Constant
      //In This Case We Upload Image In A Folder Named "images" in the webroot
      define('UPLOADPATH','images/');
      define('MAXSIZE','50786');
      if(isset($_POST['upload'])) {
      $image=$_FILES['image']['name'];
      $imagemime=$_FILES['image']['type'];
      $imagesize=$_FILES['image']['size'];
      //rename the image name to avoid duplication
      $modimage=md5('$image');
      $finalname=$modimage'.'$imagemime;
      
      if(!empty($finalname) && !empty($imagemime) $$ !empty($imagesize)) {
      //allow only jpg and gif extension and file greater than 0 bytes
      if(($imagemime == 'image/jpg' || $imagemime == 'image/gif') && $imagesize > 0 && $imagesize <= MAXSIZE && $_FILES['image']['error'] == 0) {
      $throw=UPLOADPATH.$finalname;
      if(move_uploaded_file($_FILES['image']['tmp_name'],$throw)) {
      $dbc=mysqli_connect('localhost','username','password','dbname');
      $query="INSERT INTO image (image_id,image_name,image_mime,image_size) VALUES (0,'$finalname','$imagemime','$imagesize')";
      mysqli_query($dbc,$query) ordie('Error Querying MySql');
      echo '<p> Your Image '.$image.'Has Been SuccessFully Uploaded.</p><br />';
      echo '<img src="'.UPLOADPATH.$finalname.'"/>';
      $image='';
      mysqli_close($dbc);
      } 
      else {
      echo '<p>Problem Encountered !!</p>';
      unlink($_FILES['image'][tmp_name']);
      }
      }
       else {
         echo '<p>Your Image Should Be Either Of JPEG OR GIF Format And Less Than 50 KB</p>';
       }
      }
      ?>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
      <input type="hidden"  name="MAXSIZE" value="<?php echo MAXSIZE; ?>" />
      <br />
      Choose Image :<br />
      
      <input type="file" name="image" /> &nbsp;
      <input type="submit" value="Upload" name="upload" />
      </form>
      </body>
      </html>
      Code:
      ### CREATE TABLE image In Database ###
      CREATE TABLE 'image' (
      'image_id' INT AUTO_INCREMENT,
      'image_name' VARCHAR(48),
      'image_mime' VARCHAR(8),
      'image_size' VARCHAR(8),
      PRIMARY KEY('image_id')
      );
      Last edited by Niheel; Sep 24 '10, 06:02 PM. Reason: Merged solution into one post. In the future please post your solutions in one post when possible. It's much easier to understand for readers and the person asking the question. Thanks.

      Comment

      Working...