Juszeil Conception

Juszeil Conception

  • D
  • ï
  • s T
  • a

«2017 - 3»
@GT|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
TODAY

Hide Banner | LOGIN
2023-3-21 |

BLOG

Total found 1 articles on 2017-3-25.

程 式 語 言 2017-3-25 19:02:39

PHP - header:下載與轉址等等

header函數的加bbcode_right]用

header — Send a raw HTTP header

透過header函數我們可以修改 HTTP Header
而要讓瀏覽器啟動下載必要的一行函數

header('Content-type:application/force-download');

只要有這行,就可以把瀏覽器上的output變成一個file下載
而我最常用的範例如下

header('Content-type:application/force-download'); //告訴瀏覽器 為下載
header('Content-Transfer-Encoding: Binary'); //編碼方式
header('Content-Disposition:attachment;filename='.$filename); //檔名
@readfile($filename);

透過readfile把檔案內容全部輸出到瀏覽器後,透過header的修正,就會變成下載檔案的行為
同時也可以用echo來輸出一般文字檔

舉個簡單的例子

header('Content-type:application/force-download'); //告訴瀏覽器 為下載
header('Content-Transfer-Encoding: Binary'); //編碼方式
header('Content-Disposition:attachment;filename=hello.txt'); //檔名
echo 'Hello PHP';

上面例子會通知瀏覽器下載檔案,檔名為hello.txt




這是比較簡便的做法,完整一點的例子如下,可以控制傳輸方式,檔案size等資訊

$filename = "theDownloadedFileIsCalledThis.mp3";
$myFile = "/absolute/path/to/my/file.mp3";

$mm_type="application/octet-stream";

header("Cache-Control: public, must-revalidate");
header("Pragma: hack"); // WTF? oh well, it works...
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($myFile)) );
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary\n");
readfile($myFile);
?>

又或者可以控制檔案讀取方式

// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
//header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('2007.pdf');

上面這段程式碼
如果加了header('Content-Disposition: attachment; filename="downloaded.pdf"');
這行會變成通知瀏覽器下載此pdf
如果不加,則是告訴瀏覽器output是pdf檔,瀏覽器會使用他預設讀取pdf的程式如adobe reader來作存取,如下圖是不加的執行結果


header還有訥bbcode_right]多奇妙的加bbcode_right]用
像是防止網頁過期資訊

警告: 網頁已經過期 已經使用您在表格傳送的資訊,來建立您要求的網頁。這個網頁已經無法再使用。基於安全性考量,Internet Explorer 不會自動為您重新傳送資訊。

可以加上下面這行址令解決

header ('Cache-Control: private, pre-check=0, post-check=0, max-age=0');




同時可以用他達到轉址的效果如下

header("Location: http://www.google.com.tw");


不過要記得一件事,使用header轉址的時候記得要加上exit或flush
來防止無法控制的結果

考慮下面一段程式碼

header("Location: http://www.google.com.tw");
sleep(1);
header("Location: http://www.yahoo.com.tw");

他最後結果會到yahoo而非google,因為header下了以後不會馬上執行
結果又執行了一次header函數把原先的目標給蒜bbcode_right]掉了
但是改成

header("Location: http://www.google.com.tw");
flush();
sleep(1);
header("Location: http://www.yahoo.com.tw");

或是

header("Location: http://www.google.com.tw");
exit;
sleep(1);
header("Location: http://www.yahoo.com.tw");

就可以了,不過比較建議用第二種
如果用flush要避免flush之後再去修改header,譬如說

flush();
header("Location: http://www.yahoo.com.tw");


執行上面的程式會出現以下訊息

Cannot modify header information - headers already sent

Relate Post : PHP - Resize and crop image from center PHP - run on background MyISAM 和 InnoDB 講解 PHP - 如何找出兩個日期間差了多少"月份" PHP取整數函數的四種方法 PHP - 網址參數的抓法 (使用 http_build_query) 使用PHP导入和导出CSV文件 PHP - 如何讓 PHP 的產生的陣列(array) 轉成 JavaScript 可以使用的物件(Object)格式 (透過 json_encode() 轉換 PHP - 如何產生跨年度的月份表 PHP - APC module Web Cache
Comments :
No Comments

Post your comment:


Post your comment by Guest :
Verify Code :


Back To Top

Find Me

Powered By 2013-2015 ©. Juszeil Conception version 2.0
Queries Executed : 0.0108 seconds