Store Images in MySQL Database
Asked Answered
R

1

6

I want to know how to store images and files in a MySQL Database.

I want to get images and files like that www.example/rsrc.php/example-image.jpg

example on Facebook: facebook-example.com/rsrc.php/v2/yw/r/svhh826BLKd.png

Ruler answered 12/9, 2013 at 16:47 Comment(2)
Storing images in the database is a bad idea. Store references to where they're stored on the filesystem or an object store like Amazon S3.Waneta
I have a social network, and i want to make it like facebook rsrc.php file :) any way, i want to make a CDN for the profile pictures and every picture in my networkRuler
V
14

Create a BLOB column in database table,

probably mediumblob.

but it is good to keep the photo in directory and keep the path in the database. Cheers

 CREATE TABLE tblname(ID INT,IMAGE BLOB);

INSERT INTO tblname(ID,IMAGE) VALUES(1,LOAD_FILE('C:/test.txt'));

added the answer from the comments to this question in the answer box..

Vile answered 12/9, 2013 at 16:57 Comment(5)
How do i create that BLOB column table ? :)Ruler
CREATE TABLE tblname(ID INT ,IMAGE BLOB);Vile
for testing you can insert with this. INSERT INTO tblname(ID,IMAGE) VALUES(1,LOAD_FILE('C:/test.txt'));Vile
Okay, how to get the photo in php ? the photo were placed in the DB ?Ruler
suppose this is ur html tag and want to display image database using PHP. hope it hepls. <img src="get.php?id=1"/> <?php $id = $_GET['id']; // do some validation here to ensure id is safe $link = mysql_connect("localhost", "root", ""); mysql_select_db("dvddb"); $sql = "SELECT image FROM photos WHERE id=$id"; $result = mysql_query("$sql"); $row = mysql_fetch_assoc($result); mysql_close($link); header("Content-type: image/jpeg"); echo $row['image']; ?>Vile

© 2022 - 2024 — McMap. All rights reserved.