php, mysql, images and BLOB problems

Discussion in 'Mac Programming' started by Quboid, Jul 28, 2008.

  1. Quboid macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #1
    Hello everyone,

    I am building a web application for my school (I am learning php as i go along) and I am storing some images into a DB table as BLOBs. I am trying to retrieve them now, and I have a script that seems to work but for one problem
    when i echo the variable where i store the blob it returns the binary data even when I add the header needed that should return the image, i get a bunch of weird code on the page.

    I am aware that storing the images in a file system is more efficient, But I just wanted to see this work.


    thanks in advance...


    heres the code

    Code:
    
    <?php
    
    mysql_connect(localhost,root,root);
    
    mysql_select_db(upload);
    
    
    
    
    //echo "hello";
    
    
    // I am just getting the first table row to try it out
    
    
    $query="SELECT * FROM upload";
    
    /*$query = "SELECT name, type, size, content ".
    ******** "FROM upload WHERE id = '41'";*/
    
    $result = mysql_query($query) or die('Error, query failed');
    
    $row = mysql_fetch_array($result);
    $content=$row['content'];
    $name=$row['name'];
    
    //header("Content-length: $size");
    
    //header("Content-Disposition: attachment; filename=$name");
    header("Content-type: image/jpeg"); 
    print $content;
    
    
    exit;
    ?>
    
     
  2. EliseVanLooij macrumors newbie

    Joined:
    Jul 28, 2008
    #2
    Un/Serialize

    I'm not current on blobs, but if I'm not mistaken you needed to serialize the image object when you stored it in the database, so it stands to reason you'll need to unserialize it when you retrieve it. You'll then have an image object, from which you can construct an HTML img tag.
     
  3. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #3
    You've got two header directives active

    Code:
    header('Content-type:*image/jpg');
    ...
    header("Content-type: image/jpg");
    

    Also I think mime type should be image/jpeg. I don't know if that will fix your problem though.

    b e n
     
  4. Quboid thread starter macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #4
    I removed one header,but it still doesn't work, Could it be the way I am uploading the BLOB to the table? It reads back everything else fine (id, name, etc)..
     
  5. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #5
    Which header did you keep? It should be:-

    Code:
    header("Content-type: image/jpeg");
    b e n
     
  6. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #6

Share This Page