Storing then displaying image from database

Discussion in 'PHP' started by krabople, Mar 26, 2012.

  1. #1
    Hi, wondering if somebody can tell me where I'm going wrong (I'm new to all of this). I have the following php code which uploads an image file into my database:


        //Connect to database
        include 'Resources/Include/db.inc.php';
    
        $tmp=$_FILES['image']['tmp_name'];
        //get users IP
        $ip=$_SERVER['REMOTE_ADDR'];
    
        //Don't do anything if file wasn't selected
        if (!empty($tmp)) {
    
        //Copy file to temporary folder
        copy($tmp, "./temporary/".$ip."");
    
        //open the copied image, ready to encode into text to go into the database
        $filename1 = "./temporary/".$ip;
        $fp1 = fopen($filename1, "rb");
    
        //record the image contents into a variable
        $contents1 = fread($fp1, filesize($filename1));
    
        $contents1 = addslashes($contents1);
    
        //close the file
        fclose($fp1);
    
        $ftype = $_FILES['image']['type'];
    
        //insert information into the database
        if(!mysql_query("INSERT INTO LetterImages (Data,Type,LetterID,Page)"." VALUES ( '$contents1', '$ftype',1,1)")){
        echo mysql_error();
        }
    
        //delete the temporary file we made
        unlink($filename1);
    
        }
    Code (markup):


    This seems to work ok, as when I go to the LetterImages table there is now an additional row with a file in the blob field. I then have the following code which is supposed to display the image:

    $result=mysql_query("SELECT * FROM LetterImages WHERE LetterID=1 AND Page=1");
    
        //fetch data from database
        $sqldata=mysql_fetch_array($result);
    
        $encoded=stripslashes($sqldata['Data']);
        $ftype=$sqldata['Type'];
    
        //tell the browser what type of image to display
        header("Content-type: $ftype");
    
        //decode and echo the image data
        echo $encoded;
    Code (markup):


    Instead of displaying an image, however, this just displays pages and pages of incomprehensible data. Can anybody tell me where I'm going horribly wrong?
     
    krabople, Mar 26, 2012 IP
  2. CIScripts

    CIScripts Member

    Messages:
    44
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    30
    #2
    I'm not sure if this is going to fix your problem, but one line caught my eye:

    
    
    $contents1 = addslashes($contents1);
    
    
    Code (markup):
    You have to figure out going back to original data afterwards when you return the image data ;)
     
    CIScripts, Mar 26, 2012 IP
  3. krabople

    krabople Member

    Messages:
    12
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #3
    Isn't that what this line does:

    $encoded=stripslashes($sqldata['Data']);
    Code (markup):
     
    krabople, Mar 26, 2012 IP
  4. CIScripts

    CIScripts Member

    Messages:
    44
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    30
    #4
    ahh, I wonder how I didn't notice it :D

    Can you try to echo the $ftype variable and see what it says ?
     
    CIScripts, Mar 26, 2012 IP
  5. krabople

    krabople Member

    Messages:
    12
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #5
    It says "image/jpeg" (obviously without the quotes)
     
    krabople, Mar 26, 2012 IP
  6. CIScripts

    CIScripts Member

    Messages:
    44
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    30
    #6
    I'm not sure, can you try without the addslashes/stripslashes just in case to see if it works like that ?
     
    CIScripts, Mar 26, 2012 IP
  7. krabople

    krabople Member

    Messages:
    12
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #7
    I tried it without the addslashes but it won't upload like that, it says there is a mysql syntax error
     
    krabople, Mar 26, 2012 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    One question... why in the WORLD do you want to upload the images into the database? Why not just upload them to a file-structure (folder on the webhost), and then put a LINK to said file in the database? Reduces overhead by a ton, and provides a manageable db-size...
     
    PoPSiCLe, Mar 27, 2012 IP