ホームページ  >  記事  >  バックエンド開発  >  ファイルのダウンロードプロンプトと保存を実装するためのヘッダー関数の使用法の詳細な説明

ファイルのダウンロードプロンプトと保存を実装するためのヘッダー関数の使用法の詳細な説明

伊谢尔伦
伊谢尔伦オリジナル
2017-06-24 14:16:581193ブラウズ

ファイルのダウンロード機能を実装するときに、多くの人がこの状況に遭遇すると思います。本来の目的は、ダウンロードリンクをクリックして保存ダイアログボックスを表示することですが、たとえば、PDFなどのファイルでは、保存を求めるプロンプトの代わりに、ファイルの内容をブラウザーに表示します。この問題は以下の方法で解決できる。

<?php 
$filename = &#39;路径+实际文件名&#39;; 
//文件的类型 
header(&#39;Content-type: application/pdf&#39;); 
//下载显示的名字 
header(&#39;Content-Dis
position
: attachment; filename="保存时的文件名.pdf"&#39;); 
readfile("$filename"); 
exit(); 
?>

以下はインターネット上でよく使われる方法です

if (isset($link)) 
{ 
  Header("HTTP/1.1 303 See Other"); 
  Header("Location: $link"); 
  exit; 
}

$link就是文件的实际路径。

附服务器响应HTTP的类型ContentType大全:
".*"="application/octet-stream"
".001"="application/x-001"
".301"="application/x-301"
".323"="text/h323"
".906"="application/x-906"
".907"="drawing/907"
".a11"="application/x-a11"
".acp"="audio/x-mei-aac"
".ai"="application/postscript"
".aif"="audio/aiff"
".aifc"="audio/aiff"
".aiff"="audio/aiff"
".anv"="application/x-anv"
".asa"="text/asa"
".asf"="video/x-ms-asf"
".asp"="text/asp"
".asx"="video/x-ms-asf"
".au"="audio/basic"
".avi"="video/avi"
".awf"="application/vnd.adobe.workflow"
".biz"="text/xml"
".bmp"="application/x-bmp"
".bot"="application/x-bot"
".c4t"="application/x-c4t"
".c90"="application/x-c90"
".cal"="application/x-cals"
".cat"="application/vnd.ms-pki.seccat"
".cdf"="application/x-netcdf"
".cdr"="application/x-cdr"
".cel"="application/x-cel"
".cer"="application/x-x509-ca-cert"
".cg4"="application/x-g4"
".cgm"="application/x-cgm"
".cit"="application/x-cit"
".class"="java/*"
".cml"="text/xml"
".cmp"="application/x-cmp"
".cmx"="application/x-cmx"
".cot"="application/x-cot"
".crl"="application/pkix-crl"
".crt"="application/x-x509-ca-cert"
".csi"="application/x-csi"
".css"="text/css"
".cut"="application/x-cut"
".dbf"="application/x-dbf"
".dbm"="application/x-dbm"
".dbx"="application/x-dbx"
".dcd"="text/xml"
".dcx"="application/x-dcx"
".der"="application/x-x509-ca-cert"
".dgn"="application/x-dgn"
".dib"="application/x-dib"
".dll"="application/x-msdownload"
".doc"="application/msword"
".dot"="application/msword"
".drw"="application/x-drw"
".dtd"="text/xml"
".dwf"="Model/vnd.dwf"
".dwf"="application/x-dwf"
".dwg"="application/x-dwg"
".dxb"="application/x-dxb"
".dxf"="application/x-dxf"
".edn"="application/vnd.adobe.edn"
".emf"="application/x-emf"
".eml"="message/rfc822"
".ent"="text/xml"
".epi"="application/x-epi"
".eps"="application/x-ps"
".eps"="application/postscript"
".etd"="application/x-ebx"
".exe"="application/x-msdownload"
".fax"="image/fax"
".fdf"="application/vnd.fdf"
".fif"="application/fractals"
".fo"="text/xml"
".frm"="application/x-frm"
".g4"="application/x-g4"
".gbr"="application/x-gbr"
".gcd"="application/x-gcd"
".gif"="image/gif"
".gl2"="application/x-gl2"
".gp4"="application/x-gp4"
".hgl"="application/x-hgl"
".hmr"="application/x-hmr"
".hpg"="application/x-hpgl"
".hpl"="application/x-hpl"
".hqx"="application/mac-binhex40"
".hrf"="application/x-hrf"
".hta"="application/hta"
".htc"="text/x-component"
".htm"="text/html"
".html"="text/html"
".htt"="text/webviewhtml"
".htx"="text/html"
".icb"="application/x-icb"
".ico"="image/x-icon"
"。 ico"="アプリケーション/x-ico"
".iff"="アプリケーション/x-iff"
".ig4"="アプリケーション/x-g4"
".igs"="アプリケーション/x-igs"
".iii"="アプリケーション/x-iphone"
".img"="アプリケーション/x-img"
".ins"="アプリケーション/x-internet-signup"
".isp"="アプリケーション/x -internet-signup"
".IVF"="video/x-ivf"
".java"="java/*"
".jfif"="image/jpeg"
".jpe"="image/jpeg "
".jpe"="application/x-jpe"
".jpeg"="image/jpeg"
".jpg"="image/jpeg"
".jpg"="application/x-jpg"
".js"="application/x-javascript"
".jsp"="text/html"
".la1"="audio/x-liquid-file"
".lar"="application/x -laplayer-reg"
".latex"="application/x-latex"
".lavs"="audio/x-liquid-secure"
".lbm"="application/x-lbm"
".lmsff "="audio/x-la-lms"
".ls"="application/x-javascript"
".ltr"="application/x-ltr"
".m1v"="video/x-mpeg"
".m2v"="ビデオ/x-mpeg"
".m3u"="オーディオ/mpegurl"
".m4e"="ビデオ/mpeg4"
".mac"="アプリケーション/x-mac"
" .man"="application/x-troff-man"
".math"="text/xml"
".mdb"="application/msaccess"
".mdb"="application/x-mdb"
" .mfp"="アプリケーション/x-shockwave-flash"
".mht"="メッセージ/rfc822"
".mhtml"="メッセージ/rfc822"
".mi"="アプリケーション/x-mi"
" .mid"="オーディオ/ミッド"
".midi"="オーディオ/ミッド"
".mil"="アプリケーション/x-mil"
".mml"="テキスト/xml"
".mnd"= "audio/x-musicnet-download"
".mns"="audio/x-musicnet-stream"
".mocha"="application/x-javascript"
".movi​​e"="video/x-sgi-映画"
".mp1"="オーディオ/mp1"
".mp2"="オーディオ/mp2"
".mp2v"="ビデオ/mpeg"
".mp3"="オーディオ/mp3"
".mp4 "="video/mpeg4"
".mpa"="video/x-mpg"
".mpd"="application/vnd.ms-project"
".mpe"="video/x-mpeg"
" .mpeg"="ビデオ/mpg"
".mpg"="ビデオ/mpg"
".mpga"="オーディオ/rn-mpeg"
".mpp"="アプリケーション/vnd.ms-プロジェクト"
" .mps"="video/x-mpeg"
".mpt"="application/vnd.ms-project"
".mpv"="video/mpg"
".mpv2"="video/mpeg"
" .mpw"="アプリケーション/vnd.ms-プロジェクト"
".mpx"="アプリケーション/vnd.ms-プロジェクト"
".mtx"="テキスト/xml"
".mxp"="アプリケーション/x- mmxp"
".net"="image/pnetvue"
".nrf"="application/x-nrf"
".nws"="message/rfc822"
".odc"="text/x-ms- odc"
".out"="アプリケーション/x-out"
".p10"="アプリケーション/pkcs10"
".p12"="アプリケーション/x-pkcs12"
".p7b"="アプリケーション/x- pkcs7-certificates"
".p7c"="application/pkcs7-mime"
".p7m"="application/pkcs7-mime"
".p7r"="application/x-pkcs7-certreqresp"
".p7s" ="application/pkcs7-signature"
".pc5"="application/x-pc5"
".pci"="application/x-pci"
".pcl"="application/x-pcl"
"。 pcx"="アプリケーション/x-pcx"
".pdf"="アプリケーション/pdf"
".pdf"="アプリケーション/pdf"
".pdx"="アプリケーション/vnd.adobe.pdx"
"。 pfx"="アプリケーション/x-pkcs12"
".pgl"="アプリケーション/x-pgl"
".pic"="アプリケーション/x-pic"
".pko"="アプリケーション/vnd.ms-pki .pko"
".pl"="アプリケーション/x-perl"
".plg"="text/html"
".pls"="audio/scpls"
".plt"="application/x-plt"
".png"="image/png"
".png" ="アプリケーション/x-png"
".pot"="アプリケーション/vnd.ms-powerpoint"
".ppa"="アプリケーション/vnd.ms-powerpoint"
".ppm"="アプリケーション/x-ppm "
".pps"="アプリケーション/vnd.ms-powerpoint"
".ppt"="アプリケーション/vnd.ms-powerpoint"
".ppt"="アプリケーション/x-ppt"
".pr"= "application/x-pr"
".prf"="application/pics-rules"
".prn"="application/x-prn"
".prt"="application/x-prt"
".ps "="アプリケーション/x-ps"
".ps"="アプリケーション/postscript"
".ptn"="アプリケーション/x-ptn"
".pwz"="アプリケーション/vnd.ms-powerpoint"
" .r3t"="text/vnd.rn-realtext3d"
".ra"="audio/vnd.rn-realaudio"
".ram"="audio/x-pn-realaudio"
".ras"=" application/x-ras"
".rat"="application/rat-file"
".rdf"="text/xml"
".rec"="application/vnd.rn-recording"
".red" ="アプリケーション/x-red"
".rgb"="アプリケーション/x-rgb"
".rjs"="アプリケーション/vnd.rn-realsystem-rjs"
".rjt"="アプリケーション/vnd.rn -realsystem-rjt"
".rlc"="アプリケーション/x-rlc"
".rle"="アプリケーション/x-rle"
".rm"="アプリケーション/vnd.rn-realmedia"
".rmf "="application/vnd.adobe.rmf"
".rmi"="audio/mid"
".rmj"="application/vnd.rn-realsystem-rmj"
".rmm"="audio/x- pn-realaudio"
".rmp"="アプリケーション/vnd.rn-rn_music_package"
".rms"="アプリケーション/vnd.rn-realmedia-secure"
".rmvb"="アプリケーション/vnd.rn-realmedia -vbr"
".rmx"="アプリケーション/vnd.rn-realsystem-rmx"
".rnx"="アプリケーション/vnd.rn-realplayer"
".rp"="image/vnd.rn-realpix"
".rpm"="audio/x-pn-realaudio-plugin"
".rsml"="application/vnd.rn-rsml"
".rt"="text/vnd.rn-realtext"
"。 rtf"="アプリケーション/msword"
".rtf"="アプリケーション/x-rtf"
".rv"="video/vnd.rn-realvideo"
".sam"="アプリケーション/x-sam"
".sat"="アプリケーション/x-sat"
".sdp"="アプリケーション/sdp"
".sdw"="アプリケーション/x-sdw"
".sit"="アプリケーション/x-stuffit"
".slb"="アプリケーション/x-slb"
".sld"="アプリケーション/x-sld"
".slk"="drawing/x-slk"
".smi"="アプリケーション/smil"
".smil"="アプリケーション/smil"
".smk"="アプリケーション/x-smk"
".snd"="オーディオ/ベーシック"
".sol"="テキスト/プレーン"
".sor" ="text/plain"
".spc"="application/x-pkcs7-certificates"
".spl"="application/futuresplash"
".spp"="text/xml"
".ssm"="アプリケーション/ストリーミングメディア"
".sst"="アプリケーション/vnd.ms-pki.certstore"
".stl"="アプリケーション/vnd.ms-pki.stl"
".stm"="text/html"
".sty"="アプリケーション/x-sty"
".svg"="テキスト/xml"
".swf"="アプリケーション/x-shockwave-flash"
".tdf"="アプリケーション/x-tdf "
".tg4"="アプリケーション/x-tg4"
".tga"="アプリケーション/x-tga"
".tif"="画像/tiff"
".tif"="アプリケーション/x-tif "
".tiff"="image/tiff"
".tld"="text/xml"
".top"="drawing/x-top"
".torrent"="application/x-bittorrent"
".tsd"="テキスト/xml"
".txt"="テキスト/プレーン"
".uin"="アプリケーション/x-icq"
".uls"="テキスト/iuls"
".vcf" ="text/x-vcard"
".vda"="アプリケーション/x-vda"
".vdx"="アプリケーション/vnd.visio"
".vml"="text/xml"
".vpg" ="アプリケーション/x-vpeg005"
".vsd"="アプリケーション/vnd.visio"
".vsd"="アプリケーション/x-vsd"
".vss"="アプリケーション/vnd.visio"
".vst"="アプリケーション/vnd.visio"
".vst"="アプリケーション/x-vst "
".vsw"="アプリケーション/vnd.visio"
".vsx"="アプリケーション/vnd.visio"
".vtx"="アプリケーション/vnd.visio"
".vxml"="テキスト/xml "
".wav"="オーディオ/wav"
".wax"="オーディオ/x-ms-wax"
".wb1"="アプリケーション/x-wb1"
".wb2"="アプリケーション/x -wb2"
".wb3"="アプリケーション/x-wb3"
".wbmp"="image/vnd.wap.wbmp"
".wiz"="アプリケーション/msword"
".wk3"="アプリケーション/x-wk3"
".wk4"="アプリケーション/x-wk4"
".wkq"="アプリケーション/x-wkq"
".wks"="アプリケーション/x-wks"
".wm"= "video/x-ms-wm"
".wma"="audio/x-ms-wma"
".wmd"="アプリケーション/x-ms-wmd"
".wmf"="アプリケーション/x- wmf"
".wml"="text/vnd.wap.wml"
".wmv"="video/x-ms-wmv"
".wmx"="video/x-ms-wmx"
"。 wmz"="アプリケーション/x-ms-wmz"
".wp6"="アプリケーション/x-wp6"
".wpd"="アプリケーション/x-wpd"
".wpg"="アプリケーション/x-wpg "
".wpl"="アプリケーション/vnd.ms-wpl"
".wq1"="アプリケーション/x-wq1"
".wr1"="アプリケーション/x-wr1"
".wri"="アプリケーション/x-wri"
".wrk"="アプリケーション/x-wrk"
".ws"="アプリケーション/x-ws"
".ws2"="アプリケーション/x-ws"
".wsc"= "テキスト/スクリプトレット"
".wsdl"="テキスト/xml"
".wvx"="video/x-ms-wvx"
".xdp"="アプリケーション/vnd.adobe.xdp"
".xdr "="テキスト/xml"
".xfd"="アプリケーション/vnd.adobe.xfd"
".xfdf"="アプリケーション/vnd.adobe.xfdf"
".xhtml"="テキスト/html"
" .xls"="アプリケーション/vnd.ms-excel"
".xls"="アプリケーション/x-xls"
".xlw"="アプリケーション/x-xlw"
".xml"="テキスト/xml"
".xpl"="オーディオ/scpls"
".xq"="テキスト/xml"
".xql"="テキスト/xml"
".xquery"="テキスト/xml"
".xsd "="テキスト/xml"
".xsl"="テキスト/xml"
".xslt"="テキスト/xml"
".xwd"="アプリケーション/x-xwd"
".x_b"="アプリケーション/x-x_b"
".x_t"="アプリケーション/x-x_t"

以上がファイルのダウンロードプロンプトと保存を実装するためのヘッダー関数の使用法の詳細な説明の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。