讓PHP更快的提供文件下載的代碼
<?php
$file = "/tmp/dummy.tar.gz";
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header("Content-Length: ". filesize($file));
readfile($file);
但是這個有一個問題, 就是如果文件是中文名的話, 有的用戶可能下載后的文件名是亂碼.
于是, 我們做一下修改(參考: :
<?php
$file = "/tmp/中文名.tar.gz";
$filename = basename($file);
header("Content-type: application/octet-stream");
//處理中文文件名
$ua = $_SERVER["HTTP_USER_AGENT"];
$encoded_filename = urlencode($filename);
$encoded_filename = str_replace("+", "%20", $encoded_filename);
if (preg_match("/MSIE/", $ua)) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
header("Content-Disposition: attachment; filename*=\"utf8''" . $filename . '"');
} else {
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
header('Content-Disposition: attachment; filename="' . $filename . '"');
header("Content-Length: ". filesize($file));
readfile($file);
恩, 現(xiàn)在看起來好多了, 不過還有一個問題, 那就是readfile, 雖然PHP的readfile嘗試實現(xiàn)的盡量高效, 不占用PHP本身的內(nèi)存, 但是實際上它還是需要采用MMAP(如果支持), 或者是一個固定的buffer去循環(huán)讀取文件, 直接輸出.
輸出的時候, 如果是Apache + PHP mod, 那么還需要發(fā)送到Apache的輸出緩沖區(qū). 最后才發(fā)送給用戶. 而對于Nginx + fpm如果他們分開部署的話, 那還會帶來額外的網(wǎng)絡(luò)IO.
那么, 能不能不經(jīng)過PHP這層, 直接讓W(xué)ebserver直接把文件發(fā)送給用戶呢?
今天, 我看到了一個有意思的文章: How I PHP: X-SendFile.
我們可以使用Apache的module mod_xsendfile, 讓Apache直接發(fā)送這個文件給用戶:
<?php
$file = "/tmp/中文名.tar.gz";
$filename = basename($file);
header("Content-type: application/octet-stream");
//處理中文文件名
$ua = $_SERVER["HTTP_USER_AGENT"];
$encoded_filename = urlencode($filename);
$encoded_filename = str_replace("+", "%20", $encoded_filename);
if (preg_match("/MSIE/", $ua)) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
header("Content-Disposition: attachment; filename*=\"utf8''" . $filename . '"');
} else {
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
//讓Xsendfile發(fā)送文件
header("X-Sendfile: $file");
X-Sendfile頭將被Apache處理, 并且把響應(yīng)的文件直接發(fā)送給Client.
Lighttpd和Nginx也有類似的模塊, 大家有興趣的可以去找找看
相關(guān)文章
php使用指定編碼導(dǎo)出mysql數(shù)據(jù)到csv文件的方法
這篇文章主要介紹了php使用指定編碼導(dǎo)出mysql數(shù)據(jù)到csv文件的方法,涉及php查詢mysql及操作csv文件的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03php簡單對象與數(shù)組的轉(zhuǎn)換函數(shù)代碼(php多層數(shù)組和對象的轉(zhuǎn)換)
最近用到一些簡單的對象與數(shù)組的相互轉(zhuǎn)換的問題,寫個兩個方法如下,需要的朋友可以參考下。2011-05-05phpmyadmin配置文件現(xiàn)在需要絕密的短密碼(blowfish_secret)的2種解決方法
安裝完成phpmyadmin之后,再在瀏覽器里輸入:http://localhost/phpmyadmin這時能看到phpmyadmin的管理頁面,不過會提示:“配置文件現(xiàn)在需要絕密的短密碼(blowfish_secret)?!?/div> 2014-05-05php curl發(fā)起get與post網(wǎng)絡(luò)請求案例詳解
這篇文章主要介紹了php curl發(fā)起get與post網(wǎng)絡(luò)請求案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09PHP XML error parsing SOAP payload on line 1
PHP中GBK頁面調(diào)用WebService的編碼問題:XML error parsing SOAP payload on line 12010-06-06php讀取圖片內(nèi)容并輸出到瀏覽器的實現(xiàn)代碼
如果php以圖片,zip,exe等文件輸出到瀏覽器,而前面還輸出了其他字符,那就會是你看到的亂碼2013-08-08php+js實現(xiàn)百度地圖多點標(biāo)注的方法
這篇文章主要介紹了php+js實現(xiàn)百度地圖多點標(biāo)注的方法,涉及php結(jié)合js針對百度地圖接口調(diào)用與json操作相關(guān)技巧,需要的朋友可以參考下2016-11-11最新評論