1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Storing Images In MySQL Database

Discussion in 'PHP' started by vijaykoul, Aug 17, 2005.

  1. #1
    Hi everybody,

    I want to store images directly in a MySQL database using PHP. How can I?

    Thanks in advance,
     
    vijaykoul, Aug 17, 2005 IP
  2. l234244

    l234244 Peon

    Messages:
    1,225
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Be best to search hotscripts to get a ready made image script/gallery
     
    l234244, Aug 17, 2005 IP
  3. forkqueue

    forkqueue Guest

    Messages:
    401
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you don't know how to do it, you're probably best off not doing it :p

    Unless you've got a very good reason I'd always recommend storing images as files rather than blobs. Doing so makes it much easier to move image hosting to another server/server cluster, thus giving you far more options when your site becomes popular.
     
    forkqueue, Aug 17, 2005 IP
  4. TommyD

    TommyD Peon

    Messages:
    1,397
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Forkqueue has a good point.

    vijaykoul if you share your business need with us, maybe there is a better solution, or we can help you fine-tune what you want to do.

    later,

    tom
     
    TommyD, Aug 17, 2005 IP
  5. mopacfan

    mopacfan Peon

    Messages:
    3,273
    Likes Received:
    164
    Best Answers:
    0
    Trophy Points:
    0
    #5
    forkqueue has a very good point. Blobs are difficult to work with and storing images as files is much easier and much more efficient.
     
    mopacfan, Aug 17, 2005 IP
  6. sharpweb

    sharpweb Guest

    Messages:
    246
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I had a nightmare as a new php/mysql user trying to use blobs, before realising that it'd be easier to store the images as files...
     
    sharpweb, Aug 17, 2005 IP
  7. TommyD

    TommyD Peon

    Messages:
    1,397
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Not to pick on you, just that I have users that upload pictures. Rather than having many many files in some directory, conflicts, etc.... I use blobs. This way when a user wants to be removed, I just delete his account and any blobs associated with it.

    Pretty cool thing. ;)

    tom
     
    TommyD, Aug 17, 2005 IP
  8. sharpweb

    sharpweb Guest

    Messages:
    246
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #8
    True enough, I have the same situation. I went through the trouble of deleting all the images from the server when I delete a user (or news article, or employee etc). It wasn't all that tough with a php while loop and a select statment. It was worth it for me based on the headache I was having with the blobs. But I do completly agree with your logic on that one! Good call...
     
    sharpweb, Aug 17, 2005 IP
  9. Willy

    Willy Peon

    Messages:
    281
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I'd definitely store the files in the file system instead of as blobs. You just need a naming convention that will allow you to accomplish what you want (easy enough, no conflicts).

    I really can't see any good reason to use blobs here, as it's
    a) less efficient (on all fronts: not just database blob operations vs. file system access, but also serving the image would be done more efficiently and not tying up PHP & MySQL resources if the webserver could handle it "statically")
    b) more complicated (the file system approach is simpler to code and less lines of code)
    c) just plain overkill (where is the KISS principle?)

    But of course, once upon a time, designing my own first content management systems (this would be like way back in 1997 or so), I thought it was cool, too :D

    If despite everything you go with the blob approach, at least be sure to separate the metadata (name, size, modified date, etc) of the file into a separate table than the actual blob field. (You can use the same ID in both tables.) MySQL will be eternally grateful for that small kindness at least...
     
    Willy, Aug 17, 2005 IP
  10. frankm

    frankm Active Member

    Messages:
    915
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    83
    #10
    in a somewhat related issue I did store the images in the database at first, later wanted them out (too much data in one table) so what I did was use the id and create some paths like 10/00/00/01.jpg . Store the actual image there and you can still have all meta data in the database. (id = 10000001 in this case)
     
    frankm, Aug 17, 2005 IP
  11. vijaykoul

    vijaykoul Guest

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Hi,

    But my question still remains unanswered. Can I finally do it. If Yes, how.

    Thanks in avance
     
    vijaykoul, Aug 17, 2005 IP
  12. Willy

    Willy Peon

    Messages:
    281
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Willy, Aug 17, 2005 IP