Archived

This topic is now archived and is closed to further replies.

demonrealms

PHP....MYSQL....Imaging problem!!!

Recommended Posts

demonrealms    122
Hey, I'm following this tutorial on hwo to store images in a MYSQL database with PHP. Well the problem is is that you have to echo the image out at the very beginning of the webpage. It looks like this:
<?php

// getdata.php3 - by Florian Dittmer <dittmer@gmx.net>

// Example php script to demonstrate the direct passing of binary data

// to the user. More infos at http://www.phpbuilder.com

// Syntax: getdata.php3?id=<id>


if($id) {

    // you may have to modify login information for your database server:

    @MYSQL_CONNECT("localhost","root","password");

    @mysql_select_db("binary_data");

    $query = "select bin_data,filetype from binary_data where id=$id";
    $result = @MYSQL_QUERY($query);

    $data = @MYSQL_RESULT($result,0,"bin_data");
    $type = @MYSQL_RESULT($result,0,"filetype");

    Header( "Content-type: $type");
    echo $data;

};
?>
$data is the image it echos out. But it has to be there. I want it in the body so I can eho it out into a table below. Is this possible? B/c I want it to echo out each users avitar on my forum when I echo out the thread responses. Thanks in advance. (EDIT -- fixed broken source tag. -- Kylotan.) [edited by - Kylotan on March 22, 2004 9:17:08 PM]

Share this post


Link to post
Share on other sites
Kylotan    9871
You don''t echo images out in binary form into HTML pages; you link to them using the <img> tag from the HTML. If you need to pull them from a database then that script alone is all you need. Just make sure the <img> link you use has the correct id in it.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

Share this post


Link to post
Share on other sites
Mr_Ridd    130
Just as a suggestion, wouldn''t it be better to store the path to the image in the database and then store the image on the filesystem.

Share this post


Link to post
Share on other sites
demonrealms    122
Well, I need to upload the images and store them in a database. So storing the path won''t work. I''ll try the tag though.
I never thought it would quite work that way. Thanks I''ll let you know what happens.

Share this post


Link to post
Share on other sites
Palidine    1315
usually when you upload images to a server it''ll write them to a filesystem and store the link in a database. i don''t think that databases like to have big binary data in them. we were definitely discouraged from even putting txt files into a database at my last company.

-me

Share this post


Link to post
Share on other sites
demonrealms    122
Well the
<img src="<?php echo "$data"; ?>" border=0>
didn't work. Does anyone know why? I've even(thought wan't expecting it to work, ttried this:
<img><?php echo "$data"; ?></img>
not the way it's supposed to work so wasn't surprised it didn't work. Anyone havwe any ideas? thanks.
EDIT: Let me refraise that statment. IT works, but it echos it out in binary form. Not the actually image.

[edited by - demonrealms on March 23, 2004 3:45:45 PM]

Share this post


Link to post
Share on other sites
Palidine    1315
HTML requires that you put a LINK to the image into your page. you can't output the raw image data to the html, it will never work. your browser requests a page, for each img tag it then request the image SEPERATELY from the request for the page. that's why you see images loading after the "content" has loaded.

the way you are doing it will never work. you can't store the raw data in the database unless you can address that data with a link like "http://www.myserver.com/thisImage.jpg"

that's why i said you have to store the image in the filesystem and store a LINK (aka string) in the database.

[EDIT: i no longer have the code available for how my last company did this, but essentially you have an upload receiver that receives the upload from a form post. you read in the file parts and write them out to a directory on your webserver. in our case that data was shuffled from the upload servers to the image servers by a chron job but for you that's probably not necessary. you can just write images out to an image directory and then just store the addressable path to those images into your database]

[EDIT 2: if you want to stick with you system you will have to do something really hackish like, retrieve the image data from the database and write it to a temporary file. then put an http:// stype link to that temporary file into the HTML. this is way hackish because AFAIK you can't tell when the user's browser has completed downloading the image, so you don't know when to delete the temp image. so yeah, just do it the way i said in the above edit block]

-me

[edited by - Palidine on March 23, 2004 3:46:52 PM]

[edited by - Palidine on March 23, 2004 3:49:01 PM]

Share this post


Link to post
Share on other sites
Palidine    1315
quote:
Original post by demonrealms
How would I upload it to the file system? Grab the image data, then write it to a .jpg file in the system thorough php?


well, you are already grabbing the image data from the upload if you are storing it in the database. so instead of saving it to the database, save it to the file system. i''m not sure offhand but the docs on http://www.php.net are really good.

-me

Share this post


Link to post
Share on other sites
Kylotan    9871
You don't need it on the filesystem. If you looked at the example code you're using you'd see a big hint!

Syntax: getdata.php3?id=<id>

You save this page on your server as the script that will send the image. And above is the syntax that you use in the link. eg.

<img src="getdata.php3?id=10">

...to include a link to the image stored as id=10.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]


[edited by - Kylotan on March 23, 2004 5:52:32 PM]

Share this post


Link to post
Share on other sites
Palidine    1315
quote:
Original post by Kylotan
You don''t need it on the filesystem. If you looked at the example code you''re using you''d see a big hint!



heh, my bad. didn''t realize there was another way to do this. cool.

-me

Share this post


Link to post
Share on other sites