views:

49

answers:

3

I am planing to bulid www portal in PHP, where many pictures will be stored. I decide to store pictures in directories at the server(not DB) for performence reason. Some pictures will be accessible for all users from internet, and some (if user set them private) not according to session id. What is the best solution of this problem? Perforance is important. Shoud I use some mod_rewrite or move private files to other directory than public?

John

A: 

There are two options:

  1. Store the pictures in some publicly accessible folder of your site root, name the files such that the filename cannot be guessed (randomly generated and non-sequential), and make sure directory indexing is turned off. Only serve links to images that a user is authorized to see. Provided they don't share the links to their private photos, it will be very hard for people to accidentally (or intentionally) stumble across private photos.

  2. Store the pictures outside of your site root, and serve them via a script which authenticates against the session. There are examples of how to read an image into memory and output it to the browser on the imagejpeg page of PHP.net

meagar
Ad 1. Seems to be the fastest. But aren't there some internet bots that could get the content out?What could be name of such a file md5? or something more complicated?Ad.2 Most secure, but seems that php have to take content,check,processed(header,body) and throw out picture if possible.Looks like eating resources.
john
@john though Menalto Gallery2 using [2] and pretty happy with it. Especially because of HTTP conditional get caching
Col. Shrapnel
@john Bots only follow links. If you aren't serving up links except to authenticated users, bots won't find the files. If you're worried about people finding files via bruteforce, make your filenames long and watch your logs for huge numbers of 404's in the image directory.
meagar
A: 
  1. Do not allow users to hot-link to those images. Ignoring this means you'll be allowing freeloaders to steal network resources from other legitimate users.
  2. Make sure those images are not in a Web-accessible path.
  3. Find a way to throttle traffic. You don't want one user hogging bandwidth that should have been allocated to other users.
stillstanding
A: 

Store the private files outside of your wwwroot, only accessible via a php script, that checks access rights and forwards to a 403 page or servers the image, via readfile() for example.

Maerlyn