Search code examples
phpimageuploading

How do I secure private photos that a user uploads on my site?


I am using PHP/MySQL to handle the image uploading. I want all images that are uploaded to the logged in user's gallery to only be accessible by the logged in user. I do not want people to be able to guess the file name and directly link to it.

I am thinking that I can just store the images outside the webroot and access them through some PHP. However, if the user wants to later share the image with a friend via a link, how would I allow that?

Are there any other steps I need to take to make sure only the user can see their photos? I take user privacy very seriously and want to get this right.

Thanks for your help in advance!


Solution

  • You are correct in your original assumption. Store your files outside of the public directory and use a PHP script to check authorization and display the image.

    To get around the sharing problem you can give them an area where they can say "Share this photo" and it will display a URL like

    http://www.yoursite.com/image/12390123?v=XA21IW
    

    XA21IW would be some unique hash stored in a table and they can specify a lifetime or you can code one yourself. When the page loads and v is passed in you can lookup a table to determine if it is a valid hash for that image id.

    You have some options here. Every time they click "Share this photo" you can:

    1. Destroy all old hashes
    2. Add on to the stack
    3. Allow them to configure an expiration etc...

    Or simply allow images to be public/private.