如何强制浏览器/客户端缓存下载的文件?下面是我当前的脚本,如果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
答案 0 :(得分:0)
您无法强制缓存或依赖其发生。甚至没有要求浏览器具有缓存能力。