Displaying images from MySQL database

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
dylan001
Forum Newbie
Posts: 16
Joined: Thu May 19, 2005 5:57 am

Displaying images from MySQL database

Post by dylan001 »

I'm trying to display images stored in a MySQL database (I can't avoid it!!!) They are formatted as a "BLOB", which from my understanding is the binary format of the picture.

I'm pretty new to php and don't know how to properly call and display an image from the database.

I've been able to call and display the other text coloums.

I'm guessing it's because I need to convert the binary back to the a relevant picture format somewhere along the line???

Any ideas, advice, tutorials???

Sorry if this is listed somewhere else in the forum I could find it anywhere.



Thanks.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you need to send header information down to the client about the type of information you're going to send ie: Content-Type:, Content-Length:, Content-Disposition:, etc...then just echo the data from your blob field below the header information.

see below snippet from a similar project I did a while back:

Code: Select all

// Send down the header to the client 
  Header ( "Content-Type: {$fileinfo['attachmenttype']}" ); 
  if (!empty($fileinfo['attachmentsize']) && $fileinfo['attachmentsize'] > 0)
    Header ( "Content-Length: {$fileinfo['attachmentsize']}" ); 
  if (!empty($fileinfo['attachmentfilename']) && substr($fileinfo['attachmenttype'], 0, 5) != 'image')
    Header ( "Content-Disposition: attachment; filename={$fileinfo['attachmentfilename']}" ); 

  // Loop thru and stream the nodes 1 by 1 

  for ($i = 0; $i < count($attachparts); $i++) { 
    $sql = "select filedata from ttticketattachmentsdata where id = " . $attachparts[$i]; 

    $handle = mysql_query($sql)
      or trigger_error("Couldn't get file part", E_USER_ERROR);

    $data = mysql_fetch_row($handle); 
    echo $data[0]; 
    mysql_free_result($handle);
  } 
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I'm trying to display images stored in a MySQL database (I can't avoid it!!!)
Why not?
dylan001
Forum Newbie
Posts: 16
Joined: Thu May 19, 2005 5:57 am

Post by dylan001 »

That looks like exactly what i'm after!!!

Thanks for your help
dylan001
Forum Newbie
Posts: 16
Joined: Thu May 19, 2005 5:57 am

Post by dylan001 »

Quote:
I'm trying to display images stored in a MySQL database (I can't avoid it!!!)


Why not?


well its obviously a lot easier to upload the files to the server and simply link to them, but it won't be me doing this, and the person the site is intended for won't be able to do this.

Hence a small script to create a simple web form for them to upload thier images is much easier, and hence the storage in the database.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Only reason I'm saying this is because storing images in your database can slow down you site quite a bit at times. If you don't mind me asking again.. why can't this user store files? Permission problems?
dylan001
Forum Newbie
Posts: 16
Joined: Thu May 19, 2005 5:57 am

Post by dylan001 »

Yeah, i know bout the speed problems, but i'd didn't think it would make a huge difference, its only going to be a low traffic site anyway.

I'm using the database cause its the best way I can script a solution for someone with next to no internet experience to upload their photos to a gallery site im designing.

I'm pretty new to php, so i don't know if there is an easy way to create a simple web form that will allow upload to my server (well not mine - and so i guess there may be permission restrictions) rather than the database?

Thanks for the advice.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

well to the end user, it should be sixes either way. the only difference will be on the back end (your part).

the only time you should insert files to a DB is if you can not (usually for permissions issues) save them to the HDD.

I gotta concur with JCart...do your absolute best to force the issue of using the filesystem vs the db.
dylan001
Forum Newbie
Posts: 16
Joined: Thu May 19, 2005 5:57 am

Post by dylan001 »

I gotta concur with JCart...do your absolute best to force the issue of using the filesystem vs the db.
Do you guys have any pointers on how I would go about uploading the files to the server if I didn't have any permission errors???


Thanks for the advice.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

study up on the $_FILES[] array then use the move_uploaded_file() function.

it's pretty simple really, but let me know if you have any questions.
dylan001
Forum Newbie
Posts: 16
Joined: Thu May 19, 2005 5:57 am

Post by dylan001 »

Thanks for the advice, i'll get onto that. It might take me a while but should get there eventually.


Thanks once again for your help, it's appreciated!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

http://ca3.php.net/features.file-upload should be all you need ;) good luck.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

ha! beat you to it JCart :P
Post Reply