php header函數下載檔案實現代碼_PHP教程

來源:互聯網
上載者:User
在php中header函數的使用很大,header不但可以向用戶端發送原始的 HTTP 前序資訊,同時還可以直接實現檔案下載操作,接下來小編給大家介紹歡迎各位朋友參考。

header函數最常用的不是用於下載而是用於發送http類的

跳轉

它會執行最後一個,不過是有條件的,例如:

代碼如下 複製代碼

header('Location:http://www.bKjia.c0m");
header('Location:http://www.g.cn');
header('Location:http://www.baidu.com');

這個就會跳到百度

header('Location:http://www.bKjia.c0m');echo '幫客之家;
header('Location:http://www.g.cn');
header('Location:http://www.baidu.com');

這個就會跳到google


發送狀態

輸出狀態值到瀏覽器,主要用於存取權限控制

代碼如下 複製代碼

header('HTTP/1.1 401 Unauthorized');
header('status: 401 Unauthorized');
?>

比如要限制一個使用者不能訪問該頁,則可設定狀態為404,如下所示,這樣瀏覽器就顯示為即該頁不存在

代碼如下 複製代碼

header('HTTP/1.1 404 Not Found');
header("status: 404 Not Found");
?>

下載


代碼如下 複製代碼

$filename = '路徑+實際檔案名稱';
//檔案的類型
header('Content-type: application/pdf');
//下載顯示的名字
header('Content-Disposition: attachment; filename="儲存時的檔案名稱.pdf"');
readfile("$filename");
exit();
?>

header函數進行相應的轉化,

代碼如下 複製代碼
header(‘Content-type: application/octet-stream’);//輸出的類型,根據下面提供的MIME表,選擇相應的類型
header(‘Content-Disposition: attachment; filename=”下載顯示名字.rar”‘);//下載顯示的名字
readfile(‘伺服器上的檔案名稱.rar’);//

要下的檔案,包括路徑

常用的MIME類型
.doc application/msword
.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
.rtf application/rtf
.xls application/vnd.ms-excel application/x-excel
.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.ppt application/vnd.ms-powerpoint
.pptx application/vnd.openxmlformats-officedocument.presentationml.presentation
.pps application/vnd.ms-powerpoint
.ppsx application/vnd.openxmlformats-officedocument.presentationml.slideshow
.pdf application/pdf
.swf application/x-shockwave-flash
.dll application/x-msdownload
.exe application/octet-stream
.msi application/octet-stream
.chm application/octet-stream
.cab application/octet-stream
.ocx application/octet-stream
.rar application/octet-stream
.tar application/x-tar
.tgz application/x-compressed
.zip application/x-zip-compressed
.z application/x-compress
.wav audio/wav
.wma audio/x-ms-wma
.wmv video/x-ms-wmv
.mp3 .mp2 .mpe .mpeg .mpg audio/mpeg
.rm application/vnd.rn-realmedia
.mid .midi .rmi audio/mid
.bmp image/bmp
.gif image/gif
.png image/png
.tif .tiff image/tiff
.jpe .jpeg .jpg image/jpeg
.txt text/plain
.xml text/xml
.html text/html
.css text/css
.js text/javascript

http://www.bkjia.com/PHPjc/628850.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/628850.htmlTechArticle在php中header函數的使用很大,header不但可以向用戶端發送原始的 HTTP 前序資訊,同時還可以直接實現檔案下載操作,接下來小編給大家介紹...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.