一、下载所用header头
header ( "Cache-Control: max-age=0" );
header ( "Content-Description: File Transfer" );
header ( 'Content-disposition: attachment; filename='.basename($new_file)); //文件名
header ( "Content-Type: application/png" ); //文件格式:png
header ( "Content-Transfer-Encoding: binary" ); // 告诉浏览器,这是二进制文件
header ( 'Content-Length: ' . filesize ( $new_file ) ); //告诉浏览器,文件大小
@readfile ( $new_file );//输出文件;
二、封装成方法
function down_file($url,$type='application/zip'){header("Cache-Control: public"); header("Content-Description: File Transfer"); header('Content-disposition: attachment; filename='.basename($url)); //文件名 header("Content-Type: ".$type); //zip格式的 header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件 header('Content-Length: '. filesize($url)); //告诉浏览器,文件大小 @readfile($url);
}
三、”Cache-Control: XXX”解析
请求头
max-age
客户端不愿意接受age超过这个值的缓存。并且不接受过期缓存,除非max-stale存在。 总是有值
max-stale
如果有值,客户端可以接受过期时间不超过指定值的缓存
如果没有值,客户端愿意接受过期缓存而无论过期过久。
min-fresh
客户端愿意接受一个新鲜度不小于当前age加上指定时间的响应。简单说在指定的后续一段时间内不会过期的响应。 总是有值
no-cache
客户端示意缓存,在使用缓存的时候必须进行校验。 。没有值
no-store
客户端示意缓存,不要存储本次请求的响应。但是对于已经缓存的内容则没有影响。
no-transform
不得对资源进行转换或转变。
only-if-cached
客户端只接受缓存给出的响应,如果缓存没有命中应该返回一个504
响应头
must-revalidate
一旦缓存过期,必须向源服务器进行校验,不得使用过期内容。如果无法连接必须返回504。
没有值
no-cache
如果值,在没有成功通过源站校验的情况下不得使用缓存。 有值,在进行验证的时候不要发送值指示的头域。 如Cache-Control: no-cache=”set-cookie,set-cookie2”,表示不要携带cookie进行验证。 关于带有值的情况介绍较少,这里有一个讨论:no-cache头带值时
no-store
不要缓存当前请求的响应 。没有值
no-transform
与请求头语义相同
public
任何缓存都可以进行缓存,即使响应默认是不可缓存或仅私有缓存可存的情况。
private
没有值,公有缓存不可存储;即使默认是不可缓存的,私有缓存也可以存储 。有值,将无值时的作用,限制到指定头字段上。公有有缓存不可存储指定的头字段,而其他字段可以缓存。
proxy-revalidate
与must-revalidate相同,但仅对公共缓存生效。 没有值
max-age
在经过指定时间后将过期 。有值
s-maxage
指定响应在公共缓存中的最大存活时间,它覆盖max-age和expires字段。
四、Content-Type:application/xxx解析
‘ez’ => ‘application/andrew-inset’,
‘hqx’ => ‘application/mac-binhex40’,
‘cpt’ => ‘application/mac-compactpro’,
‘doc’ => ‘application/msword’,
‘bin’ => ‘application/octet-stream’,
‘dms’ => ‘application/octet-stream’,
‘lha’ => ‘application/octet-stream’,
‘lzh’ => ‘application/octet-stream’,
‘exe’ => ‘application/octet-stream’,
‘class’ => ‘application/octet-stream’,
‘so’ => ‘application/octet-stream’,
‘dll’ => ‘application/octet-stream’,
‘oda’ => ‘application/oda’,
‘pdf’ => ‘application/pdf’,
‘ai’ => ‘application/postscript’,
‘eps’ => ‘application/postscript’,
‘ps’ => ‘application/postscript’,
‘smi’ => ‘application/smil’,
‘smil’ => ‘application/smil’,
‘mif’ => ‘application/vnd.mif’,
‘xls’ => ‘application/vnd.ms-excel’,
‘ppt’ => ‘application/vnd.ms-powerpoint’,
‘wbxml’ => ‘application/vnd.wap.wbxml’,
‘wmlc’ => ‘application/vnd.wap.wmlc’,
‘wmlsc’ => ‘application/vnd.wap.wmlscriptc’,
‘bcpio’ => ‘application/x-bcpio’,
‘vcd’ => ‘application/x-cdlink’,
‘pgn’ => ‘application/x-chess-pgn’,
‘cpio’ => ‘application/x-cpio’,
‘csh’ => ‘application/x-csh’,
‘dcr’ => ‘application/x-director’,
‘dir’ => ‘application/x-director’,
‘dxr’ => ‘application/x-director’,
‘dvi’ => ‘application/x-dvi’,
‘spl’ => ‘application/x-futuresplash’,
‘gtar’ => ‘application/x-gtar’,
‘hdf’ => ‘application/x-hdf’,
‘js’ => ‘application/x-javascript’,
‘skp’ => ‘application/x-koan’,
‘skd’ => ‘application/x-koan’,
‘skt’ => ‘application/x-koan’,
‘skm’ => ‘application/x-koan’,
‘latex’ => ‘application/x-latex’,
‘nc’ => ‘application/x-netcdf’,
‘cdf’ => ‘application/x-netcdf’,
‘sh’ => ‘application/x-sh’,
‘shar’ => ‘application/x-shar’,
‘swf’ => ‘application/x-shockwave-flash’,
‘sit’ => ‘application/x-stuffit’,
‘sv4cpio’ => ‘application/x-sv4cpio’,
‘sv4crc’ => ‘application/x-sv4crc’,
‘tar’ => ‘application/x-tar’,
‘tcl’ => ‘application/x-tcl’,
‘tex’ => ‘application/x-tex’,
‘texinfo’ => ‘application/x-texinfo’,
‘texi’ => ‘application/x-texinfo’,
‘t’ => ‘application/x-troff’,
‘tr’ => ‘application/x-troff’,
‘roff’ => ‘application/x-troff’,
‘man’ => ‘application/x-troff-man’,
‘me’ => ‘application/x-troff-me’,
‘ms’ => ‘application/x-troff-ms’,
‘ustar’ => ‘application/x-ustar’,
‘src’ => ‘application/x-wais-source’,
‘xhtml’ => ‘application/xhtml+xml’,
‘xht’ => ‘application/xhtml+xml’,
‘zip’ => ‘application/zip’,
‘au’ => ‘audio/basic’,
‘snd’ => ‘audio/basic’,
‘mid’ => ‘audio/midi’,
‘midi’ => ‘audio/midi’,
‘kar’ => ‘audio/midi’,
‘mpga’ => ‘audio/mpeg’,
‘mp2’ => ‘audio/mpeg’,
‘mp3’ => ‘audio/mpeg’,
‘aif’ => ‘audio/x-aiff’,
‘aiff’ => ‘audio/x-aiff’,
‘aifc’ => ‘audio/x-aiff’,
‘m3u’ => ‘audio/x-mpegurl’,
‘ram’ => ‘audio/x-pn-realaudio’,
‘rm’ => ‘audio/x-pn-realaudio’,
‘rpm’ => ‘audio/x-pn-realaudio-plugin’,
‘ra’ => ‘audio/x-realaudio’,
‘wav’ => ‘audio/x-wav’,
‘pdb’ => ‘chemical/x-pdb’,
‘xyz’ => ‘chemical/x-xyz’,
‘bmp’ => ‘image/bmp’,
‘gif’ => ‘image/gif’,
‘ief’ => ‘image/ief’,
‘jpeg’ => ‘image/jpeg’,
‘jpg’ => ‘image/jpeg’,
‘jpe’ => ‘image/jpeg’,
‘png’ => ‘image/png’,
‘tiff’ => ‘image/tiff’,
‘tif’ => ‘image/tiff’,
‘djvu’ => ‘image/vnd.djvu’,
‘djv’ => ‘image/vnd.djvu’,
‘wbmp’ => ‘image/vnd.wap.wbmp’,
‘ras’ => ‘image/x-cmu-raster’,
‘pnm’ => ‘image/x-portable-anymap’,
‘pbm’ => ‘image/x-portable-bitmap’,
‘pgm’ => ‘image/x-portable-graymap’,
‘ppm’ => ‘image/x-portable-pixmap’,
‘rgb’ => ‘image/x-rgb’,
‘xbm’ => ‘image/x-xbitmap’,
‘xpm’ => ‘image/x-xpixmap’,
‘xwd’ => ‘image/x-xwindowdump’,
‘igs’ => ‘model/iges’,
‘iges’ => ‘model/iges’,
‘msh’ => ‘model/mesh’,
‘mesh’ => ‘model/mesh’,
‘silo’ => ‘model/mesh’,
‘wrl’ => ‘model/vrml’,
‘vrml’ => ‘model/vrml’,
‘css’ => ‘text/css’,
‘html’ => ‘text/html’,
‘htm’ => ‘text/html’,
‘asc’ => ‘text/plain’,
‘txt’ => ‘text/plain’,
‘rtx’ => ‘text/richtext’,
‘rtf’ => ‘text/rtf’,
‘sgml’ => ‘text/sgml’,
‘sgm’ => ‘text/sgml’,
‘tsv’ => ‘text/tab-separated-values’,
‘wml’ => ‘text/vnd.wap.wml’,
‘wmls’ => ‘text/vnd.wap.wmlscript’,
‘etx’ => ‘text/x-setext’,
‘xsl’ => ‘text/xml’,
‘xml’ => ‘text/xml’,
‘mpeg’ => ‘video/mpeg’,
‘mpg’ => ‘video/mpeg’,
‘mpe’ => ‘video/mpeg’,
‘qt’ => ‘video/quicktime’,
‘mov’ => ‘video/quicktime’,
‘mxu’ => ‘video/vnd.mpegurl’,
‘avi’ => ‘video/x-msvideo’,
‘movie’ => ‘video/x-sgi-movie’,
‘ice’ => ‘x-conference/x-cooltalk’,