PHP Display image out of DB
Results 1 to 10 of 10

Thread: PHP Display image out of DB

  1. #1
    Senior Member
    Join Date
    Apr 2004
    Posts
    228

    PHP Display image out of DB

    hi guys

    I've hit another problem Got a db which holds the images I need for the site. There is a number of reasons for doing that and other options were checked out.

    I also got a script which supposed to show those images, unfortunately it only shows the first image in db unless specificaly told display another image, but I need it to desplay all the images stored or multiple entries.

    Here's the table settings:
    CREATE TABLE upload (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(30) NOT NULL,
    type VARCHAR(30) NOT NULL,
    size INT NOT NULL,
    content MEDIUMBLOB NOT NULL,
    PRIMARY KEY(id)
    );


    and here is the last variation of the code

    <?php
    $host = "localhost";
    $user = "";
    $password = "";
    $database = "";

    $connection = mysql_connect($host,$user,$password)
    or die ("Couldn't connect to a server");

    $database = mysql_select_db($database, $connection);


    $query = "SELECT id, type, content FROM upload";

    $result = mysql_query($query) or die('Error, query failed');

    if(mysql_num_rows($result) == 0)
    {
    echo "Database is empty <br>";
    }

    else
    {
    while($results = mysql_fetch_array($result))
    {
    $content = $results["content"];
    $type = $results["type"];
    $id = $results["id"];


    $image= imagecreatefromstring($content);

    switch ($type){

    case "image/jpeg":
    header("Content-type: image/jpeg");
    $display = imagejpeg ($image);
    imagedestroy($image);
    break;

    case "image/png":
    header("Content-type: image/png");
    $display = imagepng($image);
    imagedestroy($image);
    break;

    case "image/gif":
    header ("Content-type: image/gif");
    $display = imagegif($image);
    imagedestroy($image);
    break;

    }
    }
    }

    ?>


    <html>
    <head>
    <title>Download File From MySQL</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <?
    for ($i=0; i<count($display); $i++){
    echo $id;
    echo' <img src="' . $display . '" /><br />';
    }


    ?>

    </body>
    </html>



    So far there are definately 3 images in db, they are all jpegs. Any help welcome
    Last edited by nightcat; March 13th, 2007 at 12:52 PM.
    Don\'t post if you\'ve got nothing constructive to say. Flooding is annoying

  2. #2
    Senior Member Aardpsymon's Avatar
    Join Date
    Feb 2007
    Location
    St Annes (aaaa!)
    Posts
    434
    wellll.....sending multiple headers is not good. I think that is your problem right there. You need to be putting these images into a html page not just sending them over and over. Your browser will ignore the second header, or error.
    If the world doesn't stop annoying me I will name my kids ";DROP DATABASE;" and get revenge.

  3. #3
    Senior Member
    Join Date
    Apr 2004
    Posts
    228
    OK. Thanks for that

    Although it also poses a bit of a problem, how should I display images of different formats?

    Will they all display as jpegs?
    Don\'t post if you\'ve got nothing constructive to say. Flooding is annoying

  4. #4
    Senior Member Aardpsymon's Avatar
    Join Date
    Feb 2007
    Location
    St Annes (aaaa!)
    Posts
    434
    well, I would embed the images in html. Unfortunately I can't think how to do that off the top of my head, never had to do that from a db before.

    certainly if you are wanting to display these images in the same window you will have to.
    If the world doesn't stop annoying me I will name my kids ";DROP DATABASE;" and get revenge.

  5. #5
    Senior Member
    Join Date
    Apr 2004
    Posts
    228
    Yeah, most people prefer to do it from file system and then put the reference to the physical file location in db. That's the way I did it before.

    At the moment I am trying to create a set of images independent of file system.
    Don\'t post if you\'ve got nothing constructive to say. Flooding is annoying

  6. #6
    Senior Member
    Join Date
    Oct 2003
    Location
    MA
    Posts
    1,053
    Ive setup something like that for a graph would that help?

  7. #7
    Senior Member
    Join Date
    Apr 2004
    Posts
    228
    Yeah that would be greate, any samples could give me the solution
    Don\'t post if you\'ve got nothing constructive to say. Flooding is annoying

  8. #8
    AO übergeek phishphreek's Avatar
    Join Date
    Jan 2002
    Posts
    4,324
    I think this is more of what you'd want?
    http://codewalkers.com/tutorialpdfs/tutorial35.pdf
    Quitmzilla is a firefox extension that gives you stats on how long you have quit smoking, how much money you\'ve saved, how much you haven\'t smoked and recent milestones. Very helpful for people who quit smoking and used to smoke at their computers... Helps out with the urges.

  9. #9
    Senior Member
    Join Date
    Oct 2003
    Location
    MA
    Posts
    1,053

  10. #10
    Senior Member
    Join Date
    Apr 2004
    Posts
    228
    Thanks alot for all the replyes.

    Got the way the script should work now and got the basic system up. Now just working on creating my own image library
    Don\'t post if you\'ve got nothing constructive to say. Flooding is annoying

Similar Threads

  1. Apache, PHP, MySQL with basic security settings.
    By nightcat in forum The Security Tutorials Forum
    Replies: 9
    Last Post: May 28th, 2005, 02:47 AM
  2. Tutorial: Apache 2.0.49 and PHP 4.3.7!
    By Vorlin in forum Other Tutorials Forum
    Replies: 3
    Last Post: June 15th, 2004, 10:25 PM
  3. The history of the Mac line of Operating systems
    By gore in forum Operating Systems
    Replies: 3
    Last Post: March 7th, 2004, 07:02 AM
  4. Installing Apache and PHP on Linux
    By HDD in forum Other Tutorials Forum
    Replies: 2
    Last Post: February 1st, 2004, 07:05 PM
  5. Protecting Yourself From Image Theft
    By GreekGoddess in forum The Security Tutorials Forum
    Replies: 19
    Last Post: August 28th, 2003, 06:07 AM

Posting Permissions

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

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides