Jump to content

Recommended Posts

Hi guys, i run a small image hosting site, and i have a problem with caching my images.

 

I want to make the URL as short as possible, so i have a script which fakes the location of the images.

 

http://img.cx/RZVB.jpg is an image for example, but really the image is in a different location /uploads/blabla/secret/bla/RZVB.jpg (for example). My index.php script fakes this location, which works great, but browsers will not cache the image now, which is annoying for the users. Everytime I re-load an image, it gets all the data fresh from the server, instead of from the browsers cache.

 

In my index.php i read out the image like this.

 

$image_location = 'uploads/full/'.$result->image_folder.'/'.$image_id.'.'.$image_type.'';
$expires = 60*60*24*14;
header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
header('Content-Length: '.filesize($image_location).'');
header('Content-Type: image/'.$image_mime.'');
readfile($image_location); 
exit;	

 

I have also tried caching .jpg/.gif/.png files via .htaccess file, but that also did not work.

 

Im running php 5.2.8, on Cent OS 5, and apache 2.

 

Please how do I enable caching for this image forwarding that im doing?

Link to comment
https://forums.phpfreaks.com/topic/175477-image-caching-with-php-does-not-work/
Share on other sites

I have tried that before, was no go.

 

Ive have added them to my .htaccess files again. It does not work.

 

Caching DOES work when I link to the full path of the image, eg: http://img.cx/uploads/full/20090922/RZVB.jpg

 

But when i get the image via my index.php script, it always completely reloads the image.

 

Any ideas?

Its not working yet, its probably because the image is not found on that location... the header is 404, as the real image is hosted elsewhere.

 

 

http://img.cx/RZVB.jpg =

Date: Fri, 25 Sep 2009 08:36:50 GMT

Server: Apache/2.0.63 (Unix) mod_ssl/2.0.63 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 PHP/5.2.8

X-Powered-By: PHP/5.2.8

Pragma: public

Cache-Control: max-age=604800, public

Content-Length: 3429601

Keep-Alive: timeout=15, max=97

Connection: Keep-Alive

Content-Type: image/jpeg

 

404 Not Found

 

 

http://img.cx/uploads/full/20090922/RZVB.jpg =

Date: Fri, 25 Sep 2009 08:33:37 GMT

Server: Apache/2.0.63 (Unix) mod_ssl/2.0.63 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 PHP/5.2.8

Last-Modified: Tue, 22 Sep 2009 10:50:09 GMT

Etag: "2279be-3454e1-474285ca9fa40"

Accept-Ranges: bytes

Content-Length: 3429601

Cache-Control: max-age=604800, public

Content-Type: image/jpeg

 

200 OK

 

Cache probably does not work because of the 404 error, even though it does display the image correctly. But does not cache it...

 

 

Any idea what i can do now to get it working?

No im not. Dont think that is possible with the REAL path vs FAKE path.

 

http://img.cx/RZVB.jpg = FAKE path

http://img.cx/uploads/full/20090922/RZVB.jpg = REAL path

 

The '20090922' a directory with the date of the day the image was uploaded. This is done to prevent 1 directory from getting overloaded with images.

 

Or am i wrong and is it possible with mod_rewrite?

 

 

my current .htaccess

 

Options Indexes FollowSymLinks MultiViews All
php_flag register_globals on
ErrorDocument 404 /index.php

<FilesMatch "\.(jpg|jpeg|gif|ico|png)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

Ah! That's your problem, because it cannot find your fake directory and you haven't mod_rewrite it correctly, you are firing a 404 error and your index is picking it up, you then handle the URL that the error handle has provided. No matter what happens the browser will receive a 404 and will not cache the image.

 

You need to use mod_rewrite to convert your fake path to your index, like this --> index.php?file=RZVB,jpg

 

That way you can handle the script without a 404, and the browser should cache the image.

 

I'm not an expert on mod_rewrite, but there is a forum here to help :D

 

 

ILMV

Ok thanks, at least i know what im doing wrong now :)

 

So i wrote a little something, but it does not work.

 

 

.htaccess

Options Indexes FollowSymLinks MultiViews All
php_flag register_globals on
RewriteEngine On
RewriteRule  ^/([A-Z]+)\.jpg$  /pic.php?id=$1  [PT,L]

 

Shows..

 

Not Found

The requested URL /RZVB.jpg was not found on this server.

 

http://img.cx/pic.php?i=RZVB does show the image, but maybe my mod_rewrite code isnt correct.

 

Does anybody know?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.