I have images stored in a database as BLOBS with their corresponding file types being stored as well. I want to display these images on my website. How can I do this? I can't find anything the internet that talks about this directly.
Thanks
I have images stored in a database as BLOBS with their corresponding file types being stored as well. I want to display these images on my website. How can I do this? I can't find anything the internet that talks about this directly.
Thanks
A friend (who gets paids lots of money to do the backend stuff) gave me a way to go about databasing images that is far simpler than storing the actual images in a database and he says it's much faster as well.
Store the images in a folder on your server then store the image location in the database as a string.
Then instead of retriving and image from the database, you can retireve the string and put the variable into the echo of the image tag
echo'
<img src="' . $row[] . '"></img>
';
I was thinking of doing it that way...but the images are uploaded by users. Is there a way to let a user upload a file to my webspace and then store the path to it?
moonwalkercs wrote:I was thinking of doing it that way...but the images are uploaded by users. Is there a way to let a user upload a file to my webspace and then store the path to it?
Yes, there is. You need to use specific FORM method (multipart), and then fetch the file that gets stored in a temporary directory - which is handled by the server environment.
But, that has also some potential security risks, as your scripts would then have to have (public) write privilege. It depends on the server setup.
If you wish to do with database only, then to see the images you need to store their mimetype. Then write a script that will fetch the image BY ID, producing an output that has header set as mimetype of the image, followed by binary contents of the image file. For example:
<img src="image.php?id=12345" alt="Something" />
This was XML example. Omit the ending "/" for HTML 4.0. The image.php would then fetch the image from database, outputting the header and image binary, but NO HTML!
There is a short tutorial on how to handle both uploads and image fetching and display, in php documentation.