日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

关于PHP下载文件功能中header()书写方式

發布時間:2025/3/15 php 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于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加上指定時間的響應。簡單說在指定的后續一段時間內不會過期的響應。 總是有值

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’,

總結

以上是生活随笔為你收集整理的关于PHP下载文件功能中header()书写方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。