1

I have created a form that allows users to upload an image to a folder in the server. How do I insert the image into the database? I have created a table name called upload in the database named blog and a row called image with the value MediumBLOB.

If the above sounds illogical, how do i call the image from the server dynamically?

Thank You.

brad
  • 31
  • 1
  • 5
  • 1
    Is there a reason why you don't leave the image on the filesystem and just store the path or id to it in the DB? – Matthew Nov 21 '10 at 05:59
  • @konforce No, not a specific reason. Can you explain how I could store id in the db and then call the image later with the id? Thanks. Do I define the ID in the img tag? Thanks. – brad Nov 21 '10 at 06:01
  • (lower-case b), usually I just do a getimg.php?id=234234 script, and then that PHP script will either redirect to the image, return the file contents as is, or handle any cropping/resizing that may be necessary (and cache it). No matter what, it will look up the image ID in the database and find the path to the actual file on the server. – Brad Nov 21 '10 at 08:12

1 Answers1

1

See this howto. However, you should really consider using parametrized queries instead of passing the query arguments inline.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • great link. But now I am a bit confused. Should I store the complete image in the db or assign an ID to it and then call it via the ID? Thanks – brad Nov 21 '10 at 06:04
  • Most DBAs recommend against storing large blob data like images in a database, as it will unnecessarily tax the database server every time you need to fetch an image out. There are two ways to approach this: store the files in a directory inside the webroot so the web server can host them as-is, or store them somewhere else and use a script to serve them up (e.g. `image.php?id=xxx`). Either way, yes, change the actual file name to the id of the image in the DB, or come up with some other way to map database entries to filesystem entries. – cdhowie Nov 21 '10 at 06:07
  • @brad: its rare youll ever actually want to store the image itself in the db. jsut store the path on the filesystem. then when you go do display it transform that filesystem path to the public path tot he image. – prodigitalson Nov 21 '10 at 06:08
  • Whoa! Really informative. I like the idea of assigning an ID to every image. Can you provide me another useful link that would help me assign ID to images like (e.g. image.php?id=xxx) – brad Nov 21 '10 at 06:09
  • @brad: Just use an autonumber primary key column. After you insert the row, check the ID that was just added. There's your key. And note that, if possible, you should store those files in the web root so that you don't have to use a script. Using a script to serve static image data creates more unnecessary overhead. – cdhowie Nov 21 '10 at 06:10
  • Oh, I get it. Thanks. And how do i call the images later? I know it is a dumb question...sorry. – brad Nov 21 '10 at 06:13
  • Well, that's up to where you store them. If you store them in the web root, you would have some config variable somewhere, say `$IMAGE_PATH`. Then, if `$id` and `$ext` came from the db, your image path is `"$IMAGE_PATH/$id.$ext"`. If you are using a script to serve images instead, then you would have to code that path somewhere and just append the ID. – cdhowie Nov 21 '10 at 06:14
  • Grewat. Couldn't be more helpful. Thanks again. Good luck and best answer. – brad Nov 21 '10 at 06:15