tags:

views:

176

answers:

3

On a website users can upload pictures. For security reasons these are stored outside the webroot (public_html) folder. When I need to display the picture, I send the headers and have "readfile" read and output the picture data, like so:

header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header('Content-type: image/jpg');
header('Content-Length: ' . $filesize);

readfile($path_url . '/' . $photo);

This works great, but the site is growing and this is starting to be a burden on the server.

Question: is there a way to send the picture or picture data to the user, without the server first having to load the picture (obviously with the picture still being stored outside the webroot folder)?

Thanks!

David

A: 

Could you store the pictures in a SQL database?

ghoppe
I suppose I could, but currently there is over 1GB of pictures files, and mysql is pretty burdened as well. Would this really be an improvement?
David
Suppose he could. So what?
Col. Shrapnel
It's unlikely that would make things faster.
Tom
A: 

You can look into mod_xsendfile, an apache module that can sometimes be helpful in situations like yours. Otherwise, you may need to look into implementing a dedicated media server.

webbiedave
How's that help?
Col. Shrapnel
Lemme finish...
webbiedave
Thank you as well webbiedave, i'll have a look at your solution as well.
David
+1  A: 

If your problems really come from this very place and you can't use HTTP caching, there is a solution, a proxy webserver. nginx with X-Accel-Redirect or lighttpd with X-Sendfile headers

Col. Shrapnel
thanks Colonel, I'll look into this.
David