views:

39

answers:

1

Hi all..

First of all, this isn't another question about storing images on DB vs file system. I'm already storing the images on the file system. I'm just struggling to find a better way to show them to all my users.

I' currently using this system. I generate a random filename using md5(uniqueid()), the problem that I'm facing, is that the image is then presented with a link like this:

<img src="/_Media/0027a0af636c57b75472b224d432c09c.jpg" />

As you can see this isn't the prettiest way to show a image ( or a file ), and the name doesn't say anything about the image.

I've been working with a CMS system at work, that stores all uploaded files on a single table, to access that image it uses something like this:

<img src="../getattachment/94173104-e03c-499b-b41c-b25ae05a8ee1/Menu-1/Escritorios.aspx?width=175&height=175" />

As you can see the path to the image, now has a meaning compared to the other one, the problem is that this put's a big strain in the DB, for example, in the last site I made, I have an area that has around 60 images, to show the 60 images, I would have to do at least 60 individual query's to the database, besides the other query's to retrieve the various content on the page.

I think you understand my dilemma, has anyone gone trough this problem, that can give me some pointers on how to solve this?

Thanks..

A: 

You could always use .htaccess to rewrite the url and strip the friendly name. So taking your example, you could display the image source as something like:

/Media/0027a0af636c57b75472b224d432c09c/MyPictures/Venice.jpg

You could use htaccess to actually request:

/Media/0027a0af636c57b75472b224d432c09c.jpg

The other option is to have a totally friendly name:

/Media/MyPictures/Venice.jpg

And redirect it to a PHP file which examines the url and generates the hash so that it then knows the actual image file name on the server. The php script should then set the content type, read the image and output it. The major downside of this method is that you may end up with collisions as you two images may have the same hash. Given that the same thing can also occur with you current method I assume it isn't an issue.

Yacoby
The exact code I'm using to generate the filename is md5(uniqid(rand(), true)), I think it's kind of hard to have the same hash. Even with some collisions that occur in MD5..And thanks.. that is one of the way's that I'm thinking of doing, the thing that doesn't feel's right is the number of queries that I have to do..
Tio
keep in mind that each image call is another connection to the server. If you have 10 images, you will have 60 PHP processes running on the server. You're going to run out of memory without supporting too many simultaneous users.
Brent Baisley
Brent, thanks for the head's up.. that's definitely not the way to go.. I'm going to use the first option Yacoby has showed, just do a simple rewrite on the URL, with no PHP page involved.. thanks..
Tio