Jump to content

Archived

This topic is now archived and is closed to further replies.

MattEvers

Calling images from a database and assigning them a url

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

[b]What is the reason behind no images in databases?[/b]

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!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.