ダウンロードしたファイルをブラウザ/クライアントに強制的にキャッシュさせるにはどうすればよいですか? 以下は、ファイルと同じ日付のLast-Modified
場合にヘッダーを送信する現在のスクリプトです。以下のデータに見られるように、HTTP_IF_MODIFIED_SINCE
問題はHTTP_IF_MODIFIED_SINCE
決して解決されません。$_SERVER
$filename="myFile.pdf";
$filetime=filemtime($filename);
$array=array();
foreach($_SERVER as $key=>$value){syslog(LOG_INFO,$key.'=>'.print_r($value,1));}
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $filetime))
{
syslog(LOG_INFO,'Send Cache headers');
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $filetime).' GMT', true, 304);
}
else
{
//Not yet cached
syslog(LOG_INFO,'download new file');
$minutes=60*60*24*14;
header('Pragma: public');
header('Cache-Control: public');
header("Cache-Control: maxage=".$minutes);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$minutes) . ' GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $filetime).' GMT', true, 200);
header('Content-Type: application/pdf');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.$filename.';' );
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filename));
@readfile($filename);
}
$_SERVER
Jun 2 16:41:13 localhost httpd: HTTP_HOST=>example.com
Jun 2 16:41:13 localhost httpd: HTTP_USER_AGENT=>Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0
Jun 2 16:41:13 localhost httpd: HTTP_ACCEPT=>text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Jun 2 16:41:13 localhost httpd: HTTP_ACCEPT_LANGUAGE=>en-US,en;q=0.5
Jun 2 16:41:13 localhost httpd: HTTP_ACCEPT_ENCODING=>gzip, deflate
Jun 2 16:41:13 localhost httpd: HTTP_CONNECTION=>keep-alive
Jun 2 16:41:13 localhost httpd: PATH=>/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
Jun 2 16:41:13 localhost httpd: SERVER_SIGNATURE=><address>Apache/2.2.3 (CentOS) Server at example.com Port 80</address>
Jun 2 16:41:13 localhost httpd: SERVER_SOFTWARE=>Apache/2.2.3 (CentOS)
Jun 2 16:41:13 localhost httpd: SERVER_NAME=>example.com
Jun 2 16:41:13 localhost httpd: SERVER_ADDR=>192.168.0.103
Jun 2 16:41:13 localhost httpd: SERVER_PORT=>80
Jun 2 16:41:13 localhost httpd: REMOTE_ADDR=>000.000.000.000
Jun 2 16:41:13 localhost httpd: DOCUMENT_ROOT=>/var/www/html
Jun 2 16:41:13 localhost httpd: SERVER_ADMIN=>root@localhost
Jun 2 16:41:13 localhost httpd: SCRIPT_FILENAME=>/var/www/html/testing/download/index.php
Jun 2 16:41:13 localhost httpd: REMOTE_PORT=>00000
Jun 2 16:41:13 localhost httpd: GATEWAY_INTERFACE=>CGI/1.1
Jun 2 16:41:13 localhost httpd: SERVER_PROTOCOL=>HTTP/1.1
Jun 2 16:41:13 localhost httpd: REQUEST_METHOD=>GET
Jun 2 16:41:13 localhost httpd: QUERY_STRING=>
Jun 2 16:41:13 localhost httpd: REQUEST_URI=>/testing/download/
Jun 2 16:41:13 localhost httpd: SCRIPT_NAME=>/testing/download/index.php
Jun 2 16:41:13 localhost httpd: PHP_SELF=>/testing/download/index.php
Jun 2 16:41:13 localhost httpd: REQUEST_TIME=>1370216473