how to add image file/s to mysql table.I am a programmer I am using php and mysql.
3 Answers
You need to use blob (or mediumblob or longblob depending on the maximum size of images you want to support) data type for storing binary data.
Before inserting, be sure to escape special characters in the binary image data.
$img_data =mysql_real_escape_string(file_get_contents($filename));
Comments
I usually store the url of the image file rather than the file itself (not too sure how you would do that anyway). I recommend storing the url and using
9 Comments
John Parker
@JP19 Name some advantages to the BLOB approach.
Eliminate disk I/O. Faster access because of caching (I know, there exist super duper fast hard disks - but my VPS disk I/O is modest). DB storage is especially suited for favicons & thumbnails. Managing permissions is also easier (you just have to bother about DB users/perms, not file system folders). Also - I accept that the average user doesn't need DB storage of images. But there are cases where people may want it - its not as redundant as you feel imo!
John Parker
@JP19 With the exception of "managing permissions", that's all simply untrue. Read stackoverflow.com/questions/3748/…
|