Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

How to store Image into database BLOB and retrieve that image to display_image.php using new PDO

How to store Image into database BLOB and retrieve that image to display_image.php using new PDO image will be uploaded from a form I want to store image into a database and retrieve that stored image in a display_image.php or use that image anywhere in my website

Randy Hoyt

2 Answers

Images can be really large, so you'll want to store them on file and then just keep a file path (string) for it in your database.

Just do a Google search to figure out how to upload an image to your server. (You may have to configure your server to allow this.) After the image uploads, store the file path in the database.

You've got a couple options for where you store the file. Anything inside of the "htdocs" directory (as it's usually called) will be visible to the web. It's just an image file, so you can't password protect it. However, you can store the image somewhere higher than the htdocs directory, which means people can't access them directly through the internet. They only get access to them if your HTML file gives them access. (Any HTML file already has access to them, since it's on the server. It's like the difference between having access to the whole user directory and just the "shared" directory.) As long as you don't care about security, just upload the files somewhere inside your htdocs directory; it'll make things easier.

My database structure is just too large i cant just keep adding images path to each record... thanks anyways

I'm not to confident with BLOB.. but wouldn't you need the same amount of columns and tables etc for BLOB as you would a file path?

I think if you're really interested in using BLOB, surely it's no different than inserting any other length of text/string?

The user uploads their photo / you insert a photo Your app runs through the photo and creates a BLOB string The string can be inserted into the database like anything else - e.g. just like adding a product name in shirts4mike. When you call the page display_image.php, you just need to translate the string back into an image

Which step are you looking for help? Conversion to/from BLOB or the actual PDO function that inserts it into the db?

Tom Cawthorn Yes i am looking for a PDO function, i am able to store and retrieve image using old sql php connection but now as u know its deprecated, i tried uploading image into my db using pdo but its storing as BLOB 0B dat means it has no size and my fuction which i am using to retrieve image is showing broken link... and Thanks

Hi Abhijeet Naik

Can you post the pdo code you're using and any other info? Have you seen this? http://www.phpeveryday.com/articles/PDO-Working-With-BLOBs-P554.html

Hi Tom Cawthorn, yes this was the first link i went though, when i uploaded a image its storing as BLOB 0 B, no image size so no retrieval of image

Just to confirm: You upload an image, run some code to create an image BLOB, then additional code adds this to the database. In the database, the BLOB string isn't being added correctly, e.g. it hasn't got a size and is therefore empty?

I think the BLOB is just a string right? So this sounds like an issue with your conversion.. Have you checked the variables etc before submitting to database to make sure they contain what they're meant to contain?

Yeah i have checked it and used all possible ways that is used to pass the image to db but still its showing BLOB 0 B, and in db blob data type is used specifically for image storing purpose, when i used old mysql way, the image size is displaying correctly and i am also able to retrieve the image properly, but the thing is this isnt happening with PDO's, :(, and my whole website is based on PDO's and its just impossible to change back to old mysql way... Thanks again Tom Cawthorn