Insert [Bynary] Image Inside MySql Database Entry

Discussion in 'PHP' started by ColorWP.com, Jun 19, 2010.

  1. #1
    Hello.

    Let's say I have image.jpg on my server's root and I have a MySql database where I want to insert this image to (not just it's URL or filename, but the whole image's contents, and be able to retrieve it back and show it to the user without breaking).

    How do I process this with PHP?

    $contents=fread("image.jpg"); the file and the usual mysql_query("INSERT INTO blabla (img) VALUES".$contents);? I am guessing this will just insert some random string junk that will not construct the original image when read back and shown to the browser.

    Regards!
     
    ColorWP.com, Jun 19, 2010 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    binary images are stored in blob fields

    i hope it helps
    Regards

    Alex
     
    kmap, Jun 19, 2010 IP
  3. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Doing this is bad database structure; it will bloat and slow down your application in the end so try to avoid it.... Is there any reason why you must store the *file* and not its location?
     
    lukeg32, Jun 19, 2010 IP
  4. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #4
    may be he knows sometimes its done for small db

    Regards

    Alex
     
    kmap, Jun 19, 2010 IP
  5. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #5
    By their very nature, databases grow in size..... 'avoid problems later by preventing them to start with'.
     
    lukeg32, Jun 19, 2010 IP
  6. darkdrgn2k

    darkdrgn2k Active Member

    Messages:
    159
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #6
    Remember to mysql_string_escapce (? check name) your $contents first.....

    If you have allot of images sometimes your better of keeping them on the FILE SYSTEM with a path in the database :)
     
    darkdrgn2k, Jun 19, 2010 IP
  7. ColorWP.com

    ColorWP.com Notable Member

    Messages:
    3,120
    Likes Received:
    100
    Best Answers:
    1
    Trophy Points:
    270
    #7
    I am asking because I will store a lot of small images (a few kilobytes each). Is it better to store them as files instead of in the database?

    Also, nobody answered my question specifically. How do I read the contents of an image and save it in a way that I could retrieve the original later? The one thing that @kmap pointed out is that the field type must be "BLOB" instead of "VARCHAR" for example, is that correct?
     
    ColorWP.com, Jun 20, 2010 IP
  8. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    krsix, Jun 20, 2010 IP