Page 1 of 1

Displaying images stored in Databases using PHP

Posted: Wed Jul 16, 2003 10:22 am
by jamolo
:lol:
Hi guys,
I am trying to first upload images to a directory in my server then display them dynamically using php. I really would like to make it database driven but if i try the blob stuff i get funny characters displayed on my screen. Would somebody assist me with a working code. Thanks

Posted: Wed Jul 16, 2003 10:28 am
by m3rajk
umm... how owould you like the display? i have 3 pages that together are a gallery script. you place this in a directory and all sub directories are automatically turned into galleries

Posted: Wed Jul 16, 2003 10:40 am
by twigletmac
Have you made sure you are using the correct header() content-type for the type of image you are trying to display?

Mac

Posted: Thu Sep 04, 2003 6:00 am
by bluenote
:D Hi there :D ,

you might try this

Code: Select all

<?php

require_once('db_connect.inc');

$dbName = "Your_DB";

$userstable = "Your_DB_table_withimages";

MYSQL_CONNECT($hostname,$username,$password) OR DIE("Database connection failed.");

@mysql_select_db("$dbName") or die("Database not found.");

$query = "SELECT imagefield FROM $userstable [WHERE x = 'y']";

$erg = MYSQL_QUERY($query);

$image = @ mysql_fetch_array($erg);

if (!empty($image["imagefield"])) {
	
	header("Content-Type: image/jpeg");
             /* e. g. header("Content-Type: image/gif"); */
	
	echo $image["imagefield"];}
	
	else {}
	
?>
The image itself is called with the following:

Code: Select all

<?php

echo "<IMG SRC="/path_to_the/script/above.php3?x=" . y . "" ALT="some stuff about the image" BORDER=0 ALIGN=middle>\n";
I hope this will help you.

Regards, bluenote

Posted: Thu Sep 04, 2003 6:53 am
by pootergeist
putting images in a directory is considered the more professional approach (images as blobs in a database are an unneccessary resource drain as detailed here)

AW:

Posted: Thu Sep 04, 2003 7:24 am
by bluenote
Hi pootergeist,

i don't know if it is very helpful to tell the people that what they are doing isn't very professional. There is ALWAYS a better and more professional solution.

If somebody wants (or must) do things in a certain way, we should help instead of only telling him or her we know it better.

Greez, bluenote

Posted: Thu Sep 04, 2003 3:34 pm
by Unipus
Sure, but if there's a better way to do it, they should know. This example is a mistake that a lot of people seem to make (thinking that you need to store the actual image files themselves within a database). Maybe jamolo DOES need to do it this way, for some reason, but I doubt it.

I didn't find anything about pootergeist's post at all condescending or offensive. There's a better way to do things, and a good reason to do so. Why not tell him, save him some trouble?

A little education doesn't hurt.

Posted: Thu Sep 04, 2003 3:44 pm
by pootergeist
I didn't mean to be condescending and apologize if I came across that way.

I wrote the post and linked to that thread (in which I have a much harsher post btw) to try to educate and possibly prevent jamolo from making a structure mistake that he/she later regrets.

The thread and linked pages from that should hopefully point out most of the pitfalls that can be encountered in using image blobs in databases. If he/she still desires to use that method, at least he/she will understand what obstacles may lie along that path.

Posted: Fri Sep 05, 2003 5:23 am
by irealms
I did something like this in my project for custom logos.

I had an upload form placing the images in a directroy and then also entering the filename into a database (also had username and groupname as it was for multiple users/groups).

Have a display section underneath the form showing what has been uploaded.

Can't paste the code here as it's a fair size :)

but i would agree with some of the others, just upload the actual file and place the filename in the database.