<span style="color: rgb …">

網站製作學習誌

記錄學習製作網站的一切

PHP 檔案下載 Header 設置

用以下的方式,可以讓大部份瀏覽器 (主要是 IE) 詢問你是否要下載檔案 (而不是直接開啟) 。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 187);"><?php
$</span></span><font color="#009900"><strong>file_name</strong></font><span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 187);"> </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(221, 0, 0);">"file.name"</span><span style="color: rgb(0, 119, 0);">;
</span><span style="color: rgb(0, 0, 187);">$</span></span><font color="#009900"><strong>file_path</strong></font><span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 187);"> </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(221, 0, 0);">"/path/to/realfile"</span><span style="color: rgb(0, 119, 0);">;
</span><span style="color: rgb(0, 0, 187);">$</span></span><font color="#009900"><strong>file_size</strong></font><span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 187);"> </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">filesize</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$</span></span><font color="#009900"><strong>file_path</strong></font><span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 119, 0);">);
</span><span style="color: rgb(0, 0, 187);">header</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(221, 0, 0);">'Pragma: public'</span><span style="color: rgb(0, 119, 0);">);
</span><span style="color: rgb(0, 0, 187);">header</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(221, 0, 0);">'Expires: 0'</span><span style="color: rgb(0, 119, 0);">);
</span><span style="color: rgb(0, 0, 187);">header</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(221, 0, 0);">'Last-Modified: ' </span><span style="color: rgb(0, 119, 0);">. </span><span style="color: rgb(0, 0, 187);">gmdate</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(221, 0, 0);">'D, d M Y H:i '</span><span style="color: rgb(0, 119, 0);">) . </span><span style="color: rgb(221, 0, 0);">' GMT'</span><span style="color: rgb(0, 119, 0);">);
</span><span style="color: rgb(0, 0, 187);">header</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(221, 0, 0);">'Cache-Control: must-revalidate, post-check=0, pre-check=0'</span><span style="color: rgb(0, 119, 0);">);
</span><span style="color: rgb(0, 0, 187);">header</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(221, 0, 0);">'Cache-Control: private'</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">false</span><span style="color: rgb(0, 119, 0);">);
</span><span style="color: rgb(0, 0, 187);">header</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(221, 0, 0);">'Content-Type: <strong>application/octet-stream</strong>'</span><span style="color: rgb(0, 119, 0);">);
</span><span style="color: rgb(0, 0, 187);">header</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(221, 0, 0);">'Content-Length: ' </span><span style="color: rgb(0, 119, 0);">. </span><span style="color: rgb(0, 0, 187);">$</span></span><font color="#009900"><strong>file_size</strong></font><span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 119, 0);">);
</span><span style="color: rgb(0, 0, 187);">header</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(221, 0, 0);">'Content-Disposition: attachment; filename="' </span><span style="color: rgb(0, 119, 0);">. </span><span style="color: rgb(0, 0, 187);">$</span></span><font color="#009900"><strong>file_name</strong></font><span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 187);"> </span><span style="color: rgb(0, 119, 0);">. </span><span style="color: rgb(221, 0, 0);">'";'</span><span style="color: rgb(0, 119, 0);">);
</span><span style="color: rgb(0, 0, 187);">header</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(221, 0, 0);">'Content-Transfer-Encoding: <strong>binary'</strong></span><span style="color: rgb(0, 119, 0);">);
</span><span style="color: rgb(0, 0, 187);">readfile</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$</span></span><font color="#009900"><strong>file_path</strong></font><span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 119, 0);">);
</span><span style="color: rgb(0, 0, 187);">?></span></span>

補充:

看到 keisuke 誤會了以上變數的用途,我想我應該解釋一下每個變數的用意:

  • $file_name: 這是給瀏覽器看的檔案名稱,也就是下載視窗會出現的那個檔名;它可以跟實際檔案的名稱不一樣!
  • $file_path: 會連到實際檔案的位置,也就是該檔案在伺服器上的真實路徑
  • $file_size: 檔案的大小。

keisuke 誤會的是 $file_name 和 $file_path ,可能是因為他的實際檔案和下載用的 PHP 是擺在同一個位置的關係。

Comments