1

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?

3 Answers3

2

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?

Community
  • 1
  • 1
Matthew
  • 10,244
  • 5
  • 49
  • 104
1

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

Wouter Dorgelo
  • 11,770
  • 11
  • 62
  • 80
  • +1 but this is only applicable to images within his control. If his URLs are to outside resources then this will not apply.. and if there is some combination of inside/outside sources then he will have to code for discerning between the two. – Matthew Jul 02 '12 at 17:02
  • Therefor he has to make a copy of outside resources to prevent that his website depends too heavy of external resources. Makes it also easier for caching – Wouter Dorgelo Jul 02 '12 at 17:05
  • I would normally agree, but this is hard to claim because we cannot ascertain the purpose of his URL storing. Perhaps it is his *sole intent* to link to outside-sources of images... Link aggregation is certainly not out-of-the-ordinary (even though it may be frowned upon). It may even be the case that local copying/storage of images could be in violation of certain laws. I don't know and neither do you. Your explanation is great, and certainly applicable for internal storage. I was simply commenting on the possibility for when it might *not* be appropriate. – Matthew Jul 02 '12 at 17:15
  • @Mr.Pallazzo +1 your intent is the correct one. My images are not from outside sources. –  Jul 02 '12 at 20:08
  • Yea.. I saw the word `upload` and usually `upload != hotlinking` ;-) So you got what you need now, or you need more help? – Wouter Dorgelo Jul 02 '12 at 20:09
1

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.

nwalke
  • 3,170
  • 6
  • 35
  • 60