关于PHP下载文件功能中header()书写方式
一、下載所用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加上指定時間的響應。簡單說在指定的后續(xù)一段時間內(nèi)不會過期的響應。 總是有值
no-cache
客戶端示意緩存,在使用緩存的時候必須進行校驗。 。沒有值
no-store
客戶端示意緩存,不要存儲本次請求的響應。但是對于已經(jīng)緩存的內(nèi)容則沒有影響。
no-transform
不得對資源進行轉(zhuǎn)換或轉(zhuǎn)變。
only-if-cached
客戶端只接受緩存給出的響應,如果緩存沒有命中應該返回一個504
響應頭
must-revalidate
一旦緩存過期,必須向源服務器進行校驗,不得使用過期內(nèi)容。如果無法連接必須返回504。
沒有值
no-cache
如果值,在沒有成功通過源站校驗的情況下不得使用緩存。 有值,在進行驗證的時候不要發(fā)送值指示的頭域。 如Cache-Control: no-cache=”set-cookie,set-cookie2”,表示不要攜帶cookie進行驗證。 關(guān)于帶有值的情況介紹較少,這里有一個討論:no-cache頭帶值時
no-store
不要緩存當前請求的響應 。沒有值
no-transform
與請求頭語義相同
public
任何緩存都可以進行緩存,即使響應默認是不可緩存或僅私有緩存可存的情況。
private
沒有值,公有緩存不可存儲;即使默認是不可緩存的,私有緩存也可以存儲 。有值,將無值時的作用,限制到指定頭字段上。公有有緩存不可存儲指定的頭字段,而其他字段可以緩存。
proxy-revalidate
與must-revalidate相同,但僅對公共緩存生效。 沒有值
max-age
在經(jīng)過指定時間后將過期 。有值
s-maxage
指定響應在公共緩存中的最大存活時間,它覆蓋max-age和expires字段。
四、Content-Type:application/xxx解析
‘ez’ => ‘a(chǎn)pplication/andrew-inset’,
‘hqx’ => ‘a(chǎn)pplication/mac-binhex40’,
‘cpt’ => ‘a(chǎn)pplication/mac-compactpro’,
‘doc’ => ‘a(chǎn)pplication/msword’,
‘bin’ => ‘a(chǎn)pplication/octet-stream’,
‘dms’ => ‘a(chǎn)pplication/octet-stream’,
‘lha’ => ‘a(chǎn)pplication/octet-stream’,
‘lzh’ => ‘a(chǎn)pplication/octet-stream’,
‘exe’ => ‘a(chǎn)pplication/octet-stream’,
‘class’ => ‘a(chǎn)pplication/octet-stream’,
‘so’ => ‘a(chǎn)pplication/octet-stream’,
‘dll’ => ‘a(chǎn)pplication/octet-stream’,
‘oda’ => ‘a(chǎn)pplication/oda’,
‘pdf’ => ‘a(chǎn)pplication/pdf’,
‘a(chǎn)i’ => ‘a(chǎn)pplication/postscript’,
‘eps’ => ‘a(chǎn)pplication/postscript’,
‘ps’ => ‘a(chǎn)pplication/postscript’,
‘smi’ => ‘a(chǎn)pplication/smil’,
‘smil’ => ‘a(chǎn)pplication/smil’,
‘mif’ => ‘a(chǎn)pplication/vnd.mif’,
‘xls’ => ‘a(chǎn)pplication/vnd.ms-excel’,
‘ppt’ => ‘a(chǎn)pplication/vnd.ms-powerpoint’,
‘wbxml’ => ‘a(chǎn)pplication/vnd.wap.wbxml’,
‘wmlc’ => ‘a(chǎn)pplication/vnd.wap.wmlc’,
‘wmlsc’ => ‘a(chǎn)pplication/vnd.wap.wmlscriptc’,
‘bcpio’ => ‘a(chǎn)pplication/x-bcpio’,
‘vcd’ => ‘a(chǎn)pplication/x-cdlink’,
‘pgn’ => ‘a(chǎn)pplication/x-chess-pgn’,
‘cpio’ => ‘a(chǎn)pplication/x-cpio’,
‘csh’ => ‘a(chǎn)pplication/x-csh’,
‘dcr’ => ‘a(chǎn)pplication/x-director’,
‘dir’ => ‘a(chǎn)pplication/x-director’,
‘dxr’ => ‘a(chǎn)pplication/x-director’,
‘dvi’ => ‘a(chǎn)pplication/x-dvi’,
‘spl’ => ‘a(chǎn)pplication/x-futuresplash’,
‘gtar’ => ‘a(chǎn)pplication/x-gtar’,
‘hdf’ => ‘a(chǎn)pplication/x-hdf’,
‘js’ => ‘a(chǎn)pplication/x-javascript’,
‘skp’ => ‘a(chǎn)pplication/x-koan’,
‘skd’ => ‘a(chǎn)pplication/x-koan’,
‘skt’ => ‘a(chǎn)pplication/x-koan’,
‘skm’ => ‘a(chǎn)pplication/x-koan’,
‘latex’ => ‘a(chǎn)pplication/x-latex’,
‘nc’ => ‘a(chǎn)pplication/x-netcdf’,
‘cdf’ => ‘a(chǎn)pplication/x-netcdf’,
‘sh’ => ‘a(chǎn)pplication/x-sh’,
‘shar’ => ‘a(chǎn)pplication/x-shar’,
‘swf’ => ‘a(chǎn)pplication/x-shockwave-flash’,
‘sit’ => ‘a(chǎn)pplication/x-stuffit’,
‘sv4cpio’ => ‘a(chǎn)pplication/x-sv4cpio’,
‘sv4crc’ => ‘a(chǎn)pplication/x-sv4crc’,
‘tar’ => ‘a(chǎn)pplication/x-tar’,
‘tcl’ => ‘a(chǎn)pplication/x-tcl’,
‘tex’ => ‘a(chǎn)pplication/x-tex’,
‘texinfo’ => ‘a(chǎn)pplication/x-texinfo’,
‘texi’ => ‘a(chǎn)pplication/x-texinfo’,
‘t’ => ‘a(chǎn)pplication/x-troff’,
‘tr’ => ‘a(chǎn)pplication/x-troff’,
‘roff’ => ‘a(chǎn)pplication/x-troff’,
‘man’ => ‘a(chǎn)pplication/x-troff-man’,
‘me’ => ‘a(chǎn)pplication/x-troff-me’,
‘ms’ => ‘a(chǎn)pplication/x-troff-ms’,
‘ustar’ => ‘a(chǎn)pplication/x-ustar’,
‘src’ => ‘a(chǎn)pplication/x-wais-source’,
‘xhtml’ => ‘a(chǎn)pplication/xhtml+xml’,
‘xht’ => ‘a(chǎn)pplication/xhtml+xml’,
‘zip’ => ‘a(chǎn)pplication/zip’,
‘a(chǎn)u’ => ‘a(chǎn)udio/basic’,
‘snd’ => ‘a(chǎn)udio/basic’,
‘mid’ => ‘a(chǎn)udio/midi’,
‘midi’ => ‘a(chǎn)udio/midi’,
‘kar’ => ‘a(chǎn)udio/midi’,
‘mpga’ => ‘a(chǎn)udio/mpeg’,
‘mp2’ => ‘a(chǎn)udio/mpeg’,
‘mp3’ => ‘a(chǎn)udio/mpeg’,
‘a(chǎn)if’ => ‘a(chǎn)udio/x-aiff’,
‘a(chǎn)iff’ => ‘a(chǎn)udio/x-aiff’,
‘a(chǎn)ifc’ => ‘a(chǎn)udio/x-aiff’,
‘m3u’ => ‘a(chǎn)udio/x-mpegurl’,
‘ram’ => ‘a(chǎn)udio/x-pn-realaudio’,
‘rm’ => ‘a(chǎn)udio/x-pn-realaudio’,
‘rpm’ => ‘a(chǎn)udio/x-pn-realaudio-plugin’,
‘ra’ => ‘a(chǎn)udio/x-realaudio’,
‘wav’ => ‘a(chǎn)udio/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’,
‘a(chǎn)sc’ => ‘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’,
‘a(chǎn)vi’ => ‘video/x-msvideo’,
‘movie’ => ‘video/x-sgi-movie’,
‘ice’ => ‘x-conference/x-cooltalk’,
總結(jié)
以上是生活随笔為你收集整理的关于PHP下载文件功能中header()书写方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图论算法(四)--最小生成树的Krusk
- 下一篇: IDEA 配置weblogic home