HTTP ヘッダーを生成して、データをクライアントに強制的にダウンロードします。これは、ファイルのダウンロードを実装するときに役立ちます。
まず第一に、@yangweijie の成果に感謝します。これに基づいて、php_fileinfo.dll 拡張子を開く必要のないメソッドが派生しました。
方法1: 拡張機能が有効な場合//$file ファイルパス<br>
パブリック関数 download_file($file){<br>
If(is_file($file)){<br>
$length = ファイルサイズ($file);<br>
$type = mime_content_type($file);<br>
$showname = ltrim(strrchr($file,'/'),'/');<br>
header("コンテンツの説明: ファイル転送");<br>
header('Content-type: ' . $type);<br>
header('Content-Length:' . $length);<br>
If (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //IE 用<br>
header('Content-Disposition:attachment; filename="' . rawurlencode($showname) . '"');<br>
- "それ以外
header('Content-Disposition:attachment; filename="' . $showname . '"');<br>
}<br>
readfile($file);<br>
else {<br>
$this->error('ソース ファイルが存在しません!');<br>
}<br>
方法 2、php_fileinfo.dll がオンになっていない場合、誰が私たちをサーバー権限のないプログラマーにするのでしょうか、そして、悲しいことに、私たちは他人から軽蔑されることはありません!多分あなたは理解するでしょう。 <br>パブリック関数force_download($filename)
{
If ($filename == ''){<br>
falseを返します;<br>
}<br>
If (FALSE === strpos($filename, '.')){<br>
falseを返します;<br>
}<br>
$x =explode('.', $filename);<br>
$extension = end($x);<br>
$mimes =$this->getMimes();<br>
<br>
// 見つからない場合はデフォルトの MIME を設定します<br>
if ( ! isset($mimes[$extension])){<br>
$mime = 'アプリケーション/オクテットストリーム';<br>
}その他{<br>
$mime = (is_array($mimes[$extension])) $mimes[$extension][0] : $mimes[$extension];<br>
}<br>
//サーバーヘッダーを生成します<br>
If (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)<br>
{<br>
header('Content-Type: "'.$mime.'"');<br>
header('Content-Disposition:attachment; filename="'.$filename.'"');<br>
header('有効期限: 0');<br> header('Cache-Control: must-revalidate, post-check=0, pre-check=0');<br>
header("Content-Transfer-Encoding: バイナリ");<br>
header('Pragma: public');<br>
header("Content-Length: ".filesize($filename));<br>
}<br>
それ以外<br>
{<br>
header('Content-Type: "'.$mime.'"');<br>
header('Content-Disposition: attachment; filename="'.$filename.'"');<br>
header("Content-Transfer-Encoding: バイナリ");<br>
header('有効期限: 0');<br>
header('プラグマ: キャッシュなし');<br>
header("Content-Length: ".filesize($filename));<br>
}<br>
readfile($filename);<br>
}<br>
プライベート関数 getMimes(){<br>
return $mimes = array( 'hqx' => 'application/mac-binhex40',<br>
'cpt' => 'application/mac-compactpro',<br>
'csv' => array('text/x-カンマ区切り値', 'text/カンマ区切り値', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv'、'text/csv'、'application/csv'、'application/excel'、'application/vnd.msexcel')、<br>
'ビン' => 'アプリケーション/マックバイナリ',<br>
「dms」 => 'アプリケーション/オクテットストリーム',<br>
「は」 => 'アプリケーション/オクテットストリーム',<br>
'lzh' => 'アプリケーション/オクテットストリーム',<br>
「exe」 => array('application/octet-stream','application/x-msdownload'),<br>
'クラス' => 'アプリケーション/オクテットストリーム',<br>
'psd' => 'アプリケーション/x-photoshop',<br>
「だから」 => 'アプリケーション/オクテットストリーム',<br>
「海」 => 'アプリケーション/オクテットストリーム',<br>
「dll」 => 'アプリケーション/オクテットストリーム',<br>
「おだ」 => 'アプリケーション/ODA'、<br> 'pdf' => array('application/pdf','application/x-download'),<br>
「ai」 => 「申請書/追伸」、<br>
'eps' => 「申請書/追伸」、<br>
「追伸」 => 「申請書/追伸」、<br>
「スミ」 => 'アプリケーション/笑顔'、<br>
「笑顔」 => 'アプリケーション/笑顔'、<br>
「ミフ」 => 'application/vnd.mif',<br>
'xls' => array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'),<br>
'ppt' => array('application/powerpoint','application/vnd.ms-powerpoint'),<br>
'wbxml' => 'アプリケーション/wbxml',<br>
'wmlc' => 'アプリケーション/wmlc',<br>
「dcr」 => 'アプリケーション/x-director',<br>
'dir' => 'アプリケーション/x-director',<br>
「dxr」 => 'アプリケーション/x-director',<br>
'dvi' => 'アプリケーション/x-dvi',<br>
'gtar' => 'application/x-gtar',<br>
'gz' => 'application/x-gzip',<br>
'php' => 'application/x-httpd-php',<br>
'php4' => 'application/x-httpd-php',<br>
'php3' => 'application/x-httpd-php',<br>
'phtml' => 'application/x-httpd-php',<br>
'phps' => 'application/x-httpd-php-source',<br>
'js' => 'application/x-javascript',<br>
'swf' => 'application/x-shockwave-flash',<br>
「座る」 => 'application/x-stuffit',<br>
'tar' => 'アプリケーション/x-tar',<br> 'tgz' => array('application/x-tar','application/x-gzip-compressed'),<br>
'xhtml' => 'アプリケーション/xhtml+xml',<br>
'xht' => 'アプリケーション/xhtml+xml',<br>
'zip' => array('application/x-zip','application/zip','application/x-zip-compressed'),<br>
'中旬' => 「オーディオ/MIDI」、<br>
'midi' => 「オーディオ/MIDI」、<br>
'mpga' => 「オーディオ/MPEG」、<br>
'mp2' => 「オーディオ/MPEG」、<br>
「mp3」 => array('audio/mpeg','audio/mpg','audio/mpeg3','audio/mp3'),<br>
「aif」 => 「オーディオ/x-aiff」、<br>
'aiff' => 「オーディオ/x-aiff」、<br>
'aifc' => 「オーディオ/x-aiff」、<br>
「ラム」 => 'audio/x-pn-realaudio',<br>
'rm' => 'audio/x-pn-realaudio',<br>
「rpm」 => 'audio/x-pn-realaudio-plugin',<br>
「ら」 => 'audio/x-realaudio',<br>
'rv' => 'video/vnd.rn-realvideo',<br>
「wav」 => array('audio/x-wav','audio/wave','audio/wav'),<br>
'bmp' => array('image/bmp','image/x-windows-bmp'),<br>
'gif' => '画像/gif',<br>
'jpeg' => array('image/jpeg', 'image/pjpeg'),<br>
'jpg' => array('image/jpeg', 'image/pjpeg'),<br>
'jpe' => array('image/jpeg', 'image/pjpeg'),<br>
'png' => array('image/png', 'image/x-png'),<br>
'tiff' => '画像/ティフ'、<br> 'tif' => '画像/ティフ'、<br>
'css' => 'テキスト/CSS',<br>
'html' => 'テキスト/html'、<br>
'htm' => 'テキスト/html'、<br>
'shtml' => 'テキスト/html'、<br>
'txt' => 'テキスト/プレーン'、<br>
「テキスト」 => 'テキスト/プレーン'、<br>
'ログ' => array('text/plain','text/x-log'),<br>
'rtx' => 'テキスト/リッチテキスト',<br>
'rtf' => 'テキスト/rtf'、<br>
'xml' => 'テキスト/xml',<br>
'xsl' => 'テキスト/xml',<br>
'mpeg' => 'ビデオ/mpeg'、<br>
'mpg' => 'ビデオ/mpeg'、<br>
「mpe」 => 'ビデオ/mpeg'、<br>
'qt' => 「ビデオ/クイックタイム」、<br>
'mov' => 「ビデオ/クイックタイム」、<br>
'avi' => 'ビデオ/x-msvideo'、<br>
「映画」 => 'video/x-sgi-movie',<br>
'ドキュメント' => 'アプリケーション/msword'、<br>
'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'),<br>
'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'),<br>
「単語」 => array('application/msword','application/octet-stream'),<br>
'xl' => 「アプリケーション/エクセル」、<br>
'eml' => 'メッセージ/rfc822',<br>
'json' => array('application/json', 'text/json')<br>);
これを見れば誰もが理解できると思いますが、サフィックスはファイルの種類を照合するために使用されるということです。 mime_content_typeの関数を実装します。
【広告】
ショッピングモール、オフィス、コーポレートサイト、物流などのシステム開発・二次開発を請け負っています。ご相談歓迎です。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

Dreamweaver Mac版
ビジュアル Web 開発ツール

WebStorm Mac版
便利なJavaScript開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

mPDF
mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。
