Jump to content

Calling images from a database and assigning them a url


MattEvers

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.