开发者

Mapping a generic URL path to a specific file

开发者 https://www.devze.com 2023-04-01 23:49 出处:网络
I\'m writing a simple image hosting service where every image and all its thumbnails should be available via its name only, e.g. i.example.com/XXXXX.jpg. The problem with this is that every image and

I'm writing a simple image hosting service where every image and all its thumbnails should be available via its name only, e.g. i.example.com/XXXXX.jpg. The problem with this is that every image and every thumbnail would have to be in one possibly rapidly growing directory (the document root for i.example.com). I would prefer that every image is inside a separate folder for each user.

As I can't determine which image belongs to which user from the URL alone, I thought I need to write my own script which queries the database for the user and than serves the image. The problem with this开发者_如何学JAVA would be the large amount of database queries. On my site I have a gallery which displays 60 images at once, this makes 60 queries for every visitor requesting a page in the gallery.

I could use memcached, but unless I'm saving every database query result into one array, this would just replace 60 database queries with 60 memcached queries.

This all seems rather suboptimal to me, so I'm wondering if there's a better solution for this or if I should just put every image into one directory after all?


You could use the first letter, then the second etc. to partition the images based on their name. For example the image dog.jpg would be stored at /path/to/images/d/o/dog.jpg and cat.jpg at /path/to/images/c/a/cat.jpg. Your web app would need to make the path translation but it wouldn't need to hit the db. You have a problem if two people upload an image with the same name though.


You cannot put all the images in one directory if you wish to distinguish images when different people upload images with the same name. When you want to upload an image and ensure all the images belonging to a particular person keep going into the individual's directory you must use something like www.example.com/images/user_id/xxxxxxx.jpg; and you must ensure that all users log in to give their directories identity; this way the user_id directory will keep having different identities for different people whether you are accessing the database or not. Example: a user with user_id=231 will have www.example.com/images/231/xxxxxx1.jpg (or www.example.com/images/portrait_231/xxxxxx1.jpg, if you, as the developer, want to create more than one sub-directory for each user). If you still don't understand what I mean please mail me @ ike_bullocks@yahoo.com: I have been working on images for over a year now!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号