Page 1 of 1

Help - Caching a mySQL blob displayed in PHP

Posted: Wed May 24, 2006 10:35 am
by Dr.Goodvibes
I have created a web application to display images which have been stored in a mySQL blob.

To view the image I call the PHP script from within an HTML img tag.

Code: Select all

<img src='view.php?imID=2345' alt='Please cache' />
The script then retrieves the blob from the database, checks for a header type and displays the image.

No problem thus far, HOWEVER:

I am not able to cache the image.

Any ideas.


This is the header I currently have.

Code: Select all

list($width, $height, $mimeType, $attr) = getimagesize($imgBlob);
header("Content-Type: ".image_type_to_mime_type($mimeType));
header("Last-Modified: Sat, 01 Jan 2005 01:01:00 GMT"); /* updated from information by timvw */
header("Cache-Control: max-age=86400, must-revalidate");
echo $imgBlob;
I'm not sure if a php 'image' can be cached. Any thoughts?

Thanks for any help.

Posted: Wed May 24, 2006 10:38 am
by timvw
gmdate will always the current (and thus newer) date for the last-modified header...

Just output '1997-01-01 01:01:01' instead...

Working now.... I hope.

Posted: Thu May 25, 2006 2:43 am
by Dr.Goodvibes
OK. I think I solved it. :idea:

I've been messing around for the last hour or so.

Tried all sort of things which didn't work, so I continued building the overall application and ..BING.

The code I now have is as follows:

img.php (display image form DB)

Code: Select all

list($width, $height, $mimeType, $attr) = getimagesize($imgBlob);
    header("Content-Type: ".image_type_to_mime_type($mimeType));
    header("Cache-Control: max-age=86400, must-revalidate");
    header("Content-Length: ".filesize($imgBlob));
    echo $imgBlob;
As I'm building myself a application of extra ordinary magnitude, I've been adding my building blocks around the code.
One of these is the build header object. It includes:

imageCaller.php (Calls the image display script)

Code: Select all

header ("Cache-Control: cache, max-age=259200");
      $offset = 60 * 60 * 24 * 3;
      $ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
      header($ExpStr);
      header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
The above is now included in the header of the script that is calling img.php. This may have been the answer.

I know the caching times don't quite support each other, but it works. :D

Oh I think I do remember from way back that time zones can be a problem too.

I think the must-revalidate assists in renewing the cached file after the given time has expired and can be in the statement. It doesn't mean no-cache. The following document explains it better than I can.


http://www.mnot.net/cache_docs/


Why am I doing this?

Uhm, cause I can't seem to find a way to create temporary files from the web as I haven't worked out a way to enable a root process to change chmod(). Probably need to create a cgi or something. Another day.

And the images are only thumbs so they're not going to kill the mySQL cache on large queries...hopefully.

Hopefully some of this makes sense, as it's now 04:40 in the morning.