user1434156
user1434156

Reputation:

Storing image URL's in database and retrieval process

I am new to php/mysql. I am trying to store an image in my database via the URL(image location) at the moment my php code is storing the image in a folder in the directory called upload. This is insecure so i want to put the url in a database. My code is based of this IMAGEUPLOAD-WEBSITE Here is a url example generated by my code:

http://www.example.com/imageupload/uploads/medium/uniqueimagename.jpg

  1. How would i construct a valid table that will store URL's? Should it be with Varchar?
  2. How can I retrieve this url from my database and display the image? php query of the filename in the database or original url?

Upvotes: 1

Views: 11635

Answers (3)

nwalke
nwalke

Reputation: 3209

Your best bet is to use PHP to rename the file upon successful upload. Name it something that is easy to retrieve (an ID of some kind). Store that in the database, or store the whole path. Use PHP to query for either the file name or ID, then output the full path. Setting the column as a VARCHAR(2000) will work in ALL cases, but is totally unnecessary. If you use an ID instead of a drawn out file name, you can make it much shorter.

Upvotes: 1

Wouter Dorgelo
Wouter Dorgelo

Reputation: 11978

1. How would i construct a valid table that will store URL's? Should it be with Varchar?

I wouldn't store the complete path of the URL into the database. So, if you have:

http://www.example.com/imageupload/uploads/medium/uniqueimagename.jpg

I would only store:

size: 2 (medium)
name: uniqueimagename
ext: jpg

2. How can I retrieve this url from my database and display the image? php query of the filename in the database or original url?

Just fetch the data from the database and put the raw data in your html

Upvotes: 1

Matthew
Matthew

Reputation: 10444

If you intend the storage of any image URL (as opposed to just your own) then you could be safe and set the field to VARCHAR(2000) pursuant to the limit of the length of URL's by http standards:

See the following:
What is the maximum length of a URL in different browsers?

As for retrieving and displaying the image, you can simply get the value from, your DB (based on some key of your design) and use the result of the query as your image source URL.

EDIT: Here is a good exploration of this topic:
Storing Images in DB - Yea or Nay?

Upvotes: 2

Related Questions