godius Posted September 25, 2009 Share Posted September 25, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/175477-image-caching-with-php-does-not-work/ Share on other sites More sharing options...
dreamwest Posted September 25, 2009 Share Posted September 25, 2009 <FilesMatch "\.(jpg|jpeg|gif|ico|png)$"> Header set Cache-Control "max-age=604800, public" </FilesMatch> Quote Link to comment https://forums.phpfreaks.com/topic/175477-image-caching-with-php-does-not-work/#findComment-924658 Share on other sites More sharing options...
godius Posted September 25, 2009 Author Share Posted September 25, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/175477-image-caching-with-php-does-not-work/#findComment-924663 Share on other sites More sharing options...
ILMV Posted September 25, 2009 Share Posted September 25, 2009 Is there a way to check the headers? Just to make sure they are being set correctly :-) Especially Content-type Quote Link to comment https://forums.phpfreaks.com/topic/175477-image-caching-with-php-does-not-work/#findComment-924664 Share on other sites More sharing options...
dreamwest Posted September 25, 2009 Share Posted September 25, 2009 Using headers for image caching is a waste of your time - you only need htaccess. Anyway i loaded the image and it is cached, so it is working. If you refreah the page the images WILL of course reload - simple Quote Link to comment https://forums.phpfreaks.com/topic/175477-image-caching-with-php-does-not-work/#findComment-924665 Share on other sites More sharing options...
godius Posted September 25, 2009 Author Share Posted September 25, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/175477-image-caching-with-php-does-not-work/#findComment-924668 Share on other sites More sharing options...
ILMV Posted September 25, 2009 Share Posted September 25, 2009 Are you using .htaccess to redirect your URL to your index.php script? Quote Link to comment https://forums.phpfreaks.com/topic/175477-image-caching-with-php-does-not-work/#findComment-924674 Share on other sites More sharing options...
godius Posted September 25, 2009 Author Share Posted September 25, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/175477-image-caching-with-php-does-not-work/#findComment-924682 Share on other sites More sharing options...
ILMV Posted September 25, 2009 Share Posted September 25, 2009 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 ILMV Quote Link to comment https://forums.phpfreaks.com/topic/175477-image-caching-with-php-does-not-work/#findComment-924690 Share on other sites More sharing options...
godius Posted September 25, 2009 Author Share Posted September 25, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/175477-image-caching-with-php-does-not-work/#findComment-924701 Share on other sites More sharing options...
godius Posted September 25, 2009 Author Share Posted September 25, 2009 Never mind... i found it Options Indexes FollowSymLinks MultiViews All php_flag register_globals on RewriteEngine on RewriteRule RZVB(.*)\.jpg$ /index.php?i=$1 Thanks for all your help guys!! caching works perfect now. Quote Link to comment https://forums.phpfreaks.com/topic/175477-image-caching-with-php-does-not-work/#findComment-924703 Share on other sites More sharing options...
ILMV Posted September 25, 2009 Share Posted September 25, 2009 You're welcome, I used to do exactly the same, causing an error to handle funny URLs, when I learnt about mod-rewrite it made things sooo much more efficient. ILMV Quote Link to comment https://forums.phpfreaks.com/topic/175477-image-caching-with-php-does-not-work/#findComment-924705 Share on other sites More sharing options...
godius Posted September 26, 2009 Author Share Posted September 26, 2009 Seems my code is still buggy. The regex problem i have posted elsewhere: http://www.phpfreaks.com/forums/index.php/topic,270675.0.html Quote Link to comment https://forums.phpfreaks.com/topic/175477-image-caching-with-php-does-not-work/#findComment-925503 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.