# Problem displaying photos stored in MySql with php



## hamshome (Dec 28, 2007)

I have found several php scripts that allow me to insert blobs into a MySql db but have not found one to successfully retreive and display a storred photo. Any Help?


----------



## artur02 (Sep 24, 2007)

See this site: http://www.issociate.de/board/post/266297/image_into_mysql_and_out_of_mysql.html

I hope it helps.


----------



## hamshome (Dec 28, 2007)

I went to the link you gave me, and attempted to implement the code with a database set up apporpiately, and still could not get it to display the image stored in db. I got a blank page with notation, "page done but with errors". Any other suggestions? I'm tearing out the few hairs left on my mostly bald head on this one. Thanks!


----------



## Kavik79 (Jan 15, 2008)

This isn't exactly an answer to your question, but can I ask why you're saving the images directly in the database?

It seems to me you'd be better off saving the image files to a folder, then having the database store the file names to be inserted into the pages code


----------



## hamshome (Dec 28, 2007)

Kavik79 said:


> This isn't exactly an answer to your question, but can I ask why you're saving the images directly in the database?
> 
> It seems to me you'd be better off saving the image files to a folder, then having the database store the file names to be inserted into the pages code


The reason is, I'm not experienced enough at using php to figure out how to let my members upload photos, then generate a new name for the uploaded photo to store in a folder( to prevent duplication problems) and at the same time then insert the new name and path to the photo into the database, then get the photo(with new name) to display on my webpage.

It just seemed easier to put the photo directly into the db as a blob, then retreive and display it on my page along with other data stored there. If putting the photo into a folder then the db, then retreiving for display on the page would be easier or more efficient, Your help doing so would be apreciated. Thanks.


----------



## cristobal03 (Aug 5, 2005)

Yikes, blobs.

Kavik79 has it. There are a number of ways to prevent filename replication. A very simple way is to hash the file itself and rename the file to its hash + extension, then store the hash and file type along with whatever other user data you want to preserve (like a caption). PHP has built-in hashing functions (like *md5_file* or *sha1_file*).

To display the photos, you can create a control block to loop through the records in your stored files table and build *IMG* elements. I think for a presentational application you'd want to have users define a considerable amount of file information so you can populate image titles, alt attributes, etc.

*shrug*

chris.


----------



## hamshome (Dec 28, 2007)

you are way over my head and level of knowledge about php. I don't have a clue about "hashing" a folder or files, and have no expertise concerning php functions in general. I am interested in learning more about it though! I'm sure this will put me on a steep learning curve. Thanks for your suggestion!


----------



## cristobal03 (Aug 5, 2005)

Well, I guess I'll just mention then that effectively using blobs requires more technical precision than what we're suggesting. There is a considerable amount of overhead in terms of speed when calling a binary object from a database.

The simple breakdown is this: if you store your images as blobs, every time you want to access an image, you have to pull the entire image from the database--the database engine is responsible for the upload (basically). However, if you only store a file reference path, the _browser_ is responsible for the upload. And all told, browsers are better at managing uploads than database engines.

Not only that, but blobs are easily corrupted and may lead to database bloat with certain engines.

The way I look at it, databases are not file systems, so if you can avoid storing a file in a database, you should.

What's your biggest hang-up with the application Kavik suggested? From your post above, I assumed it was a matter of avoiding filename duplication. It sounded like you were already at the point where you had your database structure and your (PHP) user interface. Where exactly are you in your project?

chris.


----------



## hamshome (Dec 28, 2007)

What I'm trying to do is alow my users to upload photos to accompany ads for items they want to sell. Yes I do have a login and basic ad database in place. Now what needs to be done to complete the process of course is to effect the photo upload process insuring that each photo will be displayed with the appropriate ad. 

Ultimately, It would be great if my users could upload the photo with the other info for their ad with the photo being resized in order to keep the display page orderly, neat in appearance, and then of course be certain that the photos they desire with their ad are the ones they chose to upload. I'm very new to php programming and have been unable, after several months of grapeling with this to get it done. This is a project that hopefully will someday alow me to suppliment disability income, so it is very important to me.

I now understand and am convinced the best way to go is uploading the photo into a folder, then display them from the folder useing a link stored in the database. Makes sense! Now where do I start? Any scripting suggestions would be greatly appreciated. Again, Thanks if advance.


----------



## cristobal03 (Aug 5, 2005)

You might want to ask a moderator to move this thread into the Web Development forum. What you're requesting is a fairly common requirement, and the people who frequent that forum will be more familiar with publicly available resources--and probably better suited to answering specific PHP questions you may have.

To make the request, click the little yield icon at the top right of any of your posts. You'll be able to type a short message to the moderators. Just explain the situation and ask that they move the thread for you. Then look for it in the Web Dev forum.

If you have any questions, post back please.

chris.


----------

