displaying image from database

Discussion in 'Programming' started by dean5000v, Jul 3, 2008.

  1. #1
    well ive got this script working which inserts the image into the database.

    <?php
    $itemcode = mysql_escape_string($itemcode); 
    
    if(isset($_POST['Submit'])) {
    
    			if (!isset($_POST['itemcode']) || trim($_POST['itemcode']) == "" ) {
    				  echo "Please enter a stock code";
    }
    		else if(isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 
    
    // Temporary file name stored on the server
    $tmpName  = $_FILES['image']['tmp_name'];  
           
     // Read the file 
     $fp   = fopen($tmpName, 'r');
     $data = fread($fp, filesize($tmpName));
     $data = addslashes($data);
     fclose($fp);
          
     // Create the query and insert
     // into our database.
     $query = "INSERT INTO tbl_images ";
     $query .= "(image, itemcode) VALUES ('$data', '$itemcode')";
    	  
    $results = mysql_query($query, $connect);
          
     // Print results
     print "Thank you, your file has been uploaded.";
    } 
    		else {
    				echo 'Please select a image to upload';
    } 
    } 
    
    
    mysql_close($connect);
    ?>
    Code (markup):
    and i got this code working for displaying the image

    $result = mysql_query("SELECT * FROM tbl_images");
    while($row = mysql_fetch_array($result)){
    header('Content-type: image/jpg');
    	echo $row['image'];
    Code (markup):
    this works fine but i want ti display multiple images at the same time and i also want to insert text to the website but it wont me me because i have set the header content type to image/jpg. Does anyone know any other ways around this ??
     
    dean5000v, Jul 3, 2008 IP
  2. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #2
    Hi.

    The very first thing I will suggest you, keep the images data out of database. I did this once for one eshop and when it became larger and larger it resulted in crashes of database tables many times. Best is keep the files in a separate folder, deny access to all kind of requests, and access them through your php code. Follow this article for writing files.

    Now we come to solution of your current problem.

    The thing is, your script is sending back only one image, as
    header('Content-type: image/jpg');
    echo $row['image'];
    Code (markup):
    Solution is, create a page that has html image tags, such as

    1. main.php

    $result = mysql_query("SELECT * FROM tbl_images");
    while($row = mysql_fetch_Assoc($result)) {
     echo "
    <img src='/image_script.php?id=$row[image_id]'>
    ";
    }
    @mysql_close();
    Code (markup):
    2. image_script.php

    <?
    
       #image sending script
       $id = intval(@$_GET['id']);
       $res = mysql_query("SELECT * FROM tbl_images WHERE image_id=$id");
       if(mysql_num_rows($res)==0) {
          #send error image
          readfile("/error.png");
          @mysql_close();
          die();
       }
       #otherwise send actual image 
       $row = mysql_fetch_assoc($res);
       @mysql_close();
       header('Content-type: image/jpg');
       echo $row['image'];
    
    ?>
    Code (markup):
    IMPORTANT : Close the connection before sending the image data.


    I hope it helps.

    regards
     
    Vooler, Jul 3, 2008 IP
  3. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks alot for that, helped me loads :D
     
    dean5000v, Jul 4, 2008 IP