Database Articles

  Home arrow Database Articles arrow Page 3 - Storing Images in Database
DATABASE ARTICLES

Storing Images in Database
By: Hermawan Haryanto
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 62
    2002-12-29

    Table of Contents:
  • Storing Images in Database
  • Database Preparation
  • Start Coding
  • Viewing the Images
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Storing Images in Database - Start Coding


    (Page 3 of 5 )

    After building the table successfully, let's start doing some code. These lines below are sample of code to store image files to database.

    <?php
    // index.php - by Hermawan Haryanto &lt;hermawan@codewalkers.com&gt;
    // Example PHP Script, demonstrating Storing Image in Database
    // Detailed Information can be found at http://www.codewalkers.com

    // database connection
    $conn = mysql_connect("localhost", "username", "password") 
      OR DIE (mysql_error());
    @mysql_select_db ("database", $conn) OR DIE (mysql_error());

    // Do this process if user has browse the 
    // file and click the submit button
    if ($_FILES) {
      $image_types = Array ("image/bmp",
                            "image/jpeg",
                            "image/pjpeg",
                            "image/gif",
                            "image/x-png");
      if (is_uploaded_file ($_FILES[“userfile”][“tmp_name”])) {
        $userfile  = addslashes (fread 
                     (fopen ($_FILES["userfile"]["tmp_name"], "r"), 
                     filesize ($_FILES["userfile"]["tmp_name"])));
        $file_name = $_FILES["userfile"]["name"];
        $file_size = $_FILES["userfile"]["size"];
        $file_type = $_FILES["userfile"]["type"];

        if (in_array (strtolower ($file_type), $image_types)) {
          $sql = "INSERT INTO image "
                 . "(image_type, image, image_size, image_name, image_date) ";
          $sql.= "VALUES (";
          $sql.= "'{$file_type}', '{$userfile}', '{$file_size}', "
                 . "'{$file_name}', NOW())";
          @mysql_query ($sql, $conn);
          Header("Location:".$_SERVER["PHP_SELF"]);
          exit();
        }
      }
    }

    // Do this process of user has click 
    // a file name to view or remove
    if ($_GET) {
      $iid = $_GET["iid"];
      $act = $_GET["act"];
      switch ($act) {
        case rem:
          $sql = "DELETE FROM image WHERE image_id=$iid";
          @mysql_query ($sql, $conn);
          Header("Location:./index.php");
          exit();
          break;
        default:
          print "&lt;img src="image.php?iid=$iid"&gt;";
          break;
      }
    }

    ?>
    <html>
    <head>
    <title>Storing Images in DB</title>
    </head>
    <body>
    <form method="post" enctype="multipart/form-data">
    Select Image File: 
    <input type="file" name="userfile"  size="40">
    <input type="submit" value="submit">
    </form>
    <?php
      $sql = "SELECT * FROM image ORDER BY image_date DESC";
      $result = mysql_query ($sql, $conn);
      if (mysql_num_rows($result)&gt;0) {
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
          $i++;
          $str .= $i.". ";
          $str .= "<a href='index.php?iid=".$row["image_id"]."'>"
               . $row["image_name"]."</a> ";
          $str .= "[".$row["image_date"]."] ";
          $str .= "[".$row["image_size"]."] ";
          $str .= "[<a href='index.php?act=rem&amp;iid=".$row["image_id"]
               . "'>Remove</a>]<br>";
        }
        print $str;
      }
    ?>
    </body>
    </html>

    Upload that file, and when you execute the index.php file you'll see a simple form. With that form you'll be able to select the image file you wish to upload, then click submit. The file is uploaded to database and then you'll have a list on the bottom of the form. That's the list of files which have been uploaded to your database.

    More Database Articles Articles
    More By Hermawan Haryanto

    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual
    - Using Oracle SQL Developer with SQL Statemen...
    - Fixing Errors with Oracle SQL Developer


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap