1

how to add image file/s to mysql table.I am a programmer I am using php and mysql.

Pushan
  • 119
  • 2
  • 2
  • 6

3 Answers3

1

You shouldn't add the image itself for websites. You upload the image to the server and then save the path into the database. You should be able to then output the path of the file to HTML.

Matt Asbury
  • 5,644
  • 2
  • 21
  • 29
1

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));
0

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

Dartoxian
  • 750
  • 1
  • 6
  • 10
  • there are advantages and disadvantages to both approaches. –  Jan 04 '11 at 10:09
  • @JP19 Name some advantages to the BLOB approach. – John Parker Jan 04 '11 at 10:13
  • 1
    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! –  Jan 04 '11 at 10:17
  • @JP19 With the exception of "managing permissions", that's all simply untrue. Read http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay – John Parker Jan 04 '11 at 10:19
  • @middaparka: It is true that my VPS disk I/O is modest :) (read slow). I am also using APC cache, which makes storing favicons/thumbnails in DB even better for me. –  Jan 04 '11 at 10:22
  • @JP19 Storing them in the file system (which caches, oddly enough) will result in less disk I/O than a database. – John Parker Jan 04 '11 at 10:24
  • I did find some useful info that thread some time back - but how many people have access to the `sendfile` system call? Remember we are talking about someone for whom performance is becoming an issue. –  Jan 04 '11 at 10:25
  • @middaparka: I will have to look into what you said last. If storing in the file system does results in less disk I/O - I will rethink my own design. –  Jan 04 '11 at 10:27
  • Don't forget the security option in DB ! You can encrypt your files stored in DB. – Floccinaucinihilipilification. Aug 26 '11 at 11:59