header() 函數向客戶端發送原始的 http 報頭,認識到一點很重要,即必須在任何實際的輸出被發送之前調用 header() 函數,在 php教程 4 以及更高的版本中,您可以使用輸出緩存來解決此問題,代碼如下:
- <html>
- <?php
- // 結果出錯
- // 在調用 header() 之前已存在輸出
- header('location: http://www.49028c.com/');
- ?>
語法:header(string,replace,http_response_code)
參數 描述
string 必需,規定要發送的報頭字符串。
replace 可選,指示該報頭是否替換之前的報頭,或添加第二個報頭。
默認是 true(替換),false(允許相同類型的多個報頭).
http_response_code 可選,把 http 響應代碼強制為指定的值,php 4 以及更高版本可用.
PHP實例代碼如下:
- <?php
- function downfile()
- {
- $filename=realpath("resume.html");
- header( "content-type: application/octet-stream ");
- header( "accept-ranges: bytes ");
- header( "accept-length: " .filesize($filename));
- header( "content-disposition: attachment; filename= 4.html");
- echo file_get_contents($filename);
- readfile($filename);
- }
- downfile();
- ?>
- <?php
- function downfile($fileurl)
- {
- $filename=$fileurl;
- $file = fopen($filename, "rb");
- header( "content-type: application/octet-stream ");
- header( "accept-ranges: bytes ");
- header( "content-disposition: attachment; filename= 4.doc");
- $contents = "";
- while (!feof($file)) {
- $contents .= fread($file, 8192);
- }
- echo $contents;
- fclose($file);
- }
- $url=$_request['url'];
- $url="http://www.49028c.com";
- downfile($url);
- ?>
新聞熱點
疑難解答