Jump to content


Photo

Calling images from a database and assigning them a url


  • Please log in to reply
4 replies to this topic

#1 MattEvers

MattEvers
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 02 October 2006 - 12:20 AM

I have got a piece of code that will store a user uploaded pic into a database. Now I want to be able to assign that picture a URL to be displayed at, and give the user that URL. Just not sure how to call it from the database and create a page (with other code that will stay consistent between every picture on the page along with the picture). Thanks for any help.

#2 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 02 October 2006 - 12:47 AM

It not really a good idea to store images in a database, the only time you should ever do it is because you have to, such as when you have certain host restrictions. It would be better just to store the /path/name.ext in the database and let the server deal with the image request because it's better designed to do so.

If your still inclined to use the database, then you will need to make a image script that reads the $_GET request (/image.php?image=999) and fetches the image from the database and then dumps it to the browser using a set of header functions!


me!

#3 MattEvers

MattEvers
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 02 October 2006 - 12:50 AM

Thanks for your reply!

So when the user uploads the pic, I should have php save it in a directory on my server? Not sure how to do that... I'll have to research that.

What is the reason behind no images in databases? I'm a little new to this, so not on the same page with a lot of things just yet.

Thanks! Matt

#4 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 02 October 2006 - 01:18 AM

Image uploading isn't really that difficult. I know it can seem that way if it's your first attempted to coding it. The hardest part is validation, making sure you only allow what you want to be uploaded, because there will be a few people trying to find a flaw in logic, so as to compromise your system. There are many classes that do this, but I am one of the old schooled programmers that doesn't believe a class is the answer to everything.

What is the reason behind no images in databases?

Like with anything you do, you try your best to do it using the most logical, resource friendly way. When you store a image in the database, and serve it dynamically, you have to use (2) queries just to serve that image.'

1. get the (id) to load into the image tag
2. call the image script that runs the query to fetch the image!


The other way, you run (1) query, the server can then pass the image information to the browser (last modified). If the user has already seen the image and it is still in the browser cache, then the server doesn't have to send it again, so it becomes very resource friendly compared to you having to control the process!

me!

#5 MattEvers

MattEvers
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 02 October 2006 - 03:02 PM

That makes sense. Thanks for your help! I'm off to learn about storing/uploading images.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users