SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Storing then displaying image from database

    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:

    Code:
        //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);
    
        }


    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:


    Code:
       $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;


    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?

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,357
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    incomprehensible data
    - that is your image.

    Any particular reason for wanting to save the image into the database rather than just the path?

  3. #3
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Basically because I'm new to this and it seemed initially like the easiest option. I also think it's got to be better for referential integrity. If I can't get it to work like this, I'll probably look at doing it using a file system but as I've already developed this script it would be easier for the time being to amend it so that it works (although I have been trying to fix all day without success!).

  4. #4
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,357
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    I would suspect $ftype - try echoing it and see what it contains.

  5. #5
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried this, it says "image/jpeg" (obviously without the quotes)

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    "$contents1 = addslashes($contents1);" Why do you have addslashes?
    You script is also very very insecure.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

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