Displaying images stored in Databases using PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamolo
Forum Newbie
Posts: 1
Joined: Wed Jul 16, 2003 10:22 am
Location: Kenya
Contact:

Displaying images stored in Databases using PHP

Post 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
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
bluenote
Forum Commoner
Posts: 93
Joined: Sat Mar 01, 2003 4:59 am
Location: Heidelberg, Germany

Post 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
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post 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)
User avatar
bluenote
Forum Commoner
Posts: 93
Joined: Sat Mar 01, 2003 4:59 am
Location: Heidelberg, Germany

AW:

Post 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
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post 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.
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post 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.
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

Post 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.
Post Reply