Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2009
    Posts
    6

    Unanswered: images in mysql and php

    hi,
    not sure if im posting this in the right area but...

    This is prob really easy but im trying to store a image in mysql in a blob. Not sure how to put the image in there to start with, not sure weather to upload to webserver and put in the address to picture etc..

    And then i want to display the image using php on a webpage including other information from the database. I can get it to show all the other information in the database but not the images.

    Any help would be greatly appriciated

  2. #2
    Join Date
    Mar 2009
    Posts
    11
    Here's a quick and dirty...

    Code:
     write_image.php
    <html>
    <body>
    <?php
    mysql_connect('localhost','userid','password');
    
    $filename = '../images/ftr890x50.png';
    
    $fp = fopen($filename,'r');
    $contents = fread($fp, filesize($filename));
    fclose($fp);
    
    $query = 'insert into images.imagestore (image_name,image_type,image_bytes) values '
    .'("ftr890x50.png","png","'.$contents.'")';
    
    ?>
    
    <!-- Just read it back in into a an image element -->
    <img src="getBlobImage.php">
    
    </body>
    
    </html>
    Code:
     getBlobImage.php
    <?php
    mysql_connect('localhost','userid','password');
    
    $query = 'select image_bytes from images.imagestore where image_name = "ftr890x50.png"';
    
    $result = mysql_query($query);
    
    $image_bytes = mysql_result($result,0,'image_bytes');
    
    // This is the key to displaying the image...
    
    header("Content-type: image/png");
    header('Content-length: ' . strlen($image_bytes));
    print $image_bytes;
    
    ?>

  3. #3
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    It might also be worthwhile reading this thread to decide whether it's better to store images in the database or in the file system.

  4. #4
    Join Date
    Jan 2009
    Posts
    6
    Thanks for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •