PHP: Storing/Viewing Images Stored In A database (BLOB Data Type)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I'm a psycho - video I will show you how to use the blob data type to store images in a database and then I'm going to also show you how to retrieve that image from a database and to be shown on a page so what I want to first show you is the database first of all and this is a database structure I've just got a a table well a database craft tutorial yours will be called whatever and the table name is called blob yours might be called images are something a man in this I've got an ID which is auto increment and a primary key and I've got the name which is varchar' and thirty characters long and the image which is of a data type of blob okay so here is the page I've just have a simple or poor form here which and doesn't benefit at the moment provides go to the code and his code it's just basically a farm that palm that comes back on itself and obviously the egg type because we're dealing with files and the file import card image and just a submit button okay so now onto some code so some PHP in here one one first day is do it quick and check to see if the submit button has been pressed so if is set dollar underscore post submit and I want to do is connect to database and so you would probably and have this in an external file but just for the purpose of this tutorial I'm just going to leave it here but you would normally do something like a require and DBC OPP or connect or something like that and but I'm going to stick with this just for this tutorial so um route 27 okay and the database was called tutorial I think and yeah I think it was tutorial and so now what we want to do is get the image name so we can put this at database so my skeletons got real underscore escaping anything that goes into a database or is queried are you query a database and basically put some protection mandate which MySQL real escape string does provide some protection from my own objection and so I'm going to do it dalton's go files image and the name if you're not too familiar with file uploading and the Dalton scoff files files variable and the first an array in that is called the image which is called what we called it or PR in the file upload and we called the name image there so that's what goes in there and the name is another array within the image array so it's a multi-dimensional array and the name is basically the name if you do print underscore R and then put in that dollar underscore files with justing image in there then you will get another load of arrays within that array and and you can see we're never get name from I'm not going to show you that and so now well that gets name so now we're going to get the image data and so my Skelton's go I'm starting store files image and a mini dtmp and store name in that and that's basically the TMP and the tempering name was given money store in tempe director and but first but around this so around the mask wants go string we need the file and let's go get content because this one i'll get the content of the file and so basically if you've got a text file with say hello world in there and this will and the image data will then return hello world because we're dealing with an image it will get basically the image data and we can so if I'd say if I just echo out now image data and now I'm come back here refresh page and upload a file so if I'll upload this we get a lot of rubbish up here because this is basically the image but HTML or the yeah HTML and it doesn't really know how to interpret images or image data into an image so we after then give it a header in PHP later on to actually tell the browser this is an image and show it and okay so that's basically that just ignore that jargon for now that's that's working and that's perfect fine and so now what we need to do is get the image type smith up a bit image type and and that is basically i squat squat real I'm Scott escape drink again and in here we need dollar and score files image and type now this will basically tell us if or what type it is so basically what we can do is if if so best yeah it's basically sort of string and so we can pick out what we want from the string so if we do image type in here and then basically if we put 0 and 5 and this will pick out the first five characters of this string which if if it is an image it will be image so we can then put equals image and then echo and working code here else echo on the images are allowed this is just a just a quick check basically and what on this enough at least I'm a : it's basically if we just test this out now and so if I just refresh the page as you can see we get working code because this file I selected was an image if I choose something that's not an image so say and this text document click upload we get only images are allowed because it is not an image so that's basically just a quick check so now what we can do here if if it is an image we can now insert the image into the database so MySQL underscore query inserts into a blood bicarb database by the table aperture and values I have three now the first one was in the ID that's altering Koreans who can leave that second Muslim name so image name and the third was the image so image data can go in there and so that's all good to go so if I just refresh will save this knockout and if I choose a image upload as you see it's uploaded fine what you could do there is put say an echo to say image uploaded slightly and and if we go to the database and there's the image being put into the database there and so now what we need to do is size will show this image so to actually show an image and I basically got an empty PHP document here which is called show image PHP and now basically you need nothing in this apart from PHP code like I've got here so as you see in HTML we've got all this HTML and crap a bowl for the top and basically all this needs to be is nothing but pure PHP and otherwise this will not work properly and so first we need to do is connect to database so I'm going to copy this over here just to give it a title to save at a time okay and first of all we need to get an ID and the ID remember real is a shrink and dollar underscore get ID to avoid you passing and ma get variable to this page and so basically what you could do as a soft check again just make sure everything goes smoothly and if is set dollar underscore get ID and then put your code inside here and else you could just echo out the error or something I don't know and because you do need this ID in a get variable so if we get variable is not letting you you're gonna come up with an error basic latter a PHP error so you can lashes catch in here with us so you don't get any nasty errors and so now it's going to create query variable here which actually just queries the database for the image so my scones got a query so select all wrong blob and we're ID is equal to the ID that was passed and so now we can just get this Dayton rawrr equal you could probably use the masculine score result function for this as in you're only going to be getting one like just the image but this is just the way I prefer to get data from a database using a while loop and I'm the mouse valve fetch associative array function okay so in this we need the image data from the database so that's from the rope and an image image and filled in there so now what we need to do now is header and this basically content - its height image for / jpg this basically tells the browser now that this this page um is an image basic class so that's why we need nothing else in this apart from and just pure PHP basically so using this you see how we've got JPEG here so obviously if you've got a page PNG or gif or whatever of an image file in the database then it will not be shown so maybe what you can do is when uploading and get to the type salt like what we did before but start different and get these file extension of the file and then upload that to the database as well and then when retrieving it here you can then just put something like image type in there get the image type and then just probably image type in a variable here and to actually say to the to actually make it more dynamic because at the moment only we've had called adjust JPEGs to be allowed so that's one then you could think of is making it dynamic for other images okay so now all we need to do is echo out the image to data and that should be good to go so now if we just so basically we don't touch this page we don't actually navigate to this page in the browser if you know if you want to view through the image basically anywhere on a page so if I just come out down here and we're not there I'm here for example and if you want to actually view an image basically you have to get get the idea of the image first so and then what you do is in image in image tag basically I do is put show images or the name of that page showing mr. pitch P and then the ID and the ID of the image goes in here so thinking I was it was was it was it six I'm not too sure we'll see so you find out so now if we just refresh this page will not submit in refresh we get this image up here now alpha scientist daddy wrong here and I'm just have a check and it was actually seven in the database so if I just change this to seven and then refresh we should now where we refresh we should see the image obviously we don't like resend they've got there's the image they're being shown up there and that's basically how to store images as blob and then retrieve it from the database and and show it in a browser so basically this show image show image page shall we get a rough because and no idea is in that if we point ID of 797 there as you see the image shows up but this is based clean just no sauced you can't view any size because there's no source code of an image if nothing basically what this does is converts the page into an image of no I mean so when you sort of see in the URL at the top here like the image name JPEG this is basically what it's doing in in a way it's just not showing it as JPEG in the URL okay so I hope this helps and thanks for watching and I will see you next time
Info
Channel: abell12
Views: 302,509
Rating: undefined out of 5
Keywords:
Id: kPGxWaIhLmk
Channel Id: undefined
Length: 15min 31sec (931 seconds)
Published: Wed Apr 10 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.