[PHP] 下載檔案時無法直接開啟文件的解法方法

看標題可能看不懂我在說什麼,來看看圖吧!

以下範例自動產生 Excel 檔案,並使瀏覽器以下載方式提示使用者,而非在IE中開啟Excel表單。

在 IE 中出現提示畫面:

20041009_01.gif


這時選擇「開啟」,由 Excel 打開這份文件,結果卻是...

20041009_02.gif

(看不清楚可點圖放大)

測試用的原始碼如下:

$price_list .= "$brand_name\t$class_name\t$product_model\t$product_price\n";
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=pricelist.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $price_header."\n".$price_list;

這個發生的原因在於 output buffer 跟 Internet Explorer 暫存檔產生的時間差所造成了,這時只要利用 PHP 的 output buffer 函數來自訂產生檔案的時機即可。

ob_start();
$price_list .= "$brand_name\t$class_name\t$product_model\t$product_price\n";
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=pricelist.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $price_header."\n".$price_list;
ob_flush();

修改過後結果就正常了。
20041009_03.gif

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

你好,
剛好最近程式有用到類似功能
我希望文件能被下載而不是直接開啟
能請教是哪裡出錯了嗎?

某一段PHP
<a href='../images/pdf/downloadpdf.php?pdf_file=test.pdf' target='_blank'>。。。</a>

downloadpdf.php之內容
<?
header("Content-Disposition: attachment; filename=$pdf_file;");
?>

但是下載之檔案卻是空的, 我懷疑是路徑的問題, 但是卻不知道問題出在哪裡(test.pdf與downloadpdf.php是同一路徑)
如果不經由下載而直接開啟就沒問題
<a href='../images/pdf/test.pdf' target='_blank'>。。。</a>
困擾好久了, 感謝賜教

你好~
我的header是這樣寫的...
set_time_limit(0);
ob_start();
header("Content-disposition: filename=test.csv");
header("Content-type: application/octetstream");
header("Pragma: no-cache");
header("Expires: 0");
之前寫的程式可以下載,這之程式不知道為什麼就是一直說「無法下載」,把header蓋掉的話,資料是可以直接印出來的,麻煩你了@@給個意見吧 ><""

打以下的header 移走
header("Pragma: no-cache");
header("Expires: 0");

綜合各家所長, 我整理出來的 PHP 萬用下載 SCRIPT

//For IE 5.5 設定用
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');

header("Content-Type: application/octetstream; name=$FILEname"); //for IE & Opera

header("Content-Type: application/octet-stream; name=$FILEname"); //for the rest

header("Content-Disposition: attachment; filename=$FILEname;");

header("Content-Transfer-Encoding: binary");
header("Cache-Control: cache, must-revalidate");
header("Pragma: public");
header("Expires: 0");

Alex:
謝謝你的補充喔!^^

感謝 Alex 的無私奉獻與指教, 的確使得!

加上了ob_start();

ob_flush();
还是错误!

我用的是asp.net
遇到相同的問題
卻無法用BufferOutput解決
有人解決過嗎

加了
ob_start();
ob_flush();
還是有無法直接開啟的問題, 用了:
header("Cache-Control: cache, must-revalidate");
就可以了, thx

this my php file :
session_start();
$pschk = $_POST['pschk'];
$subroot = $_POST['subroot'];
$filen = $_POST['filen'];
$ofile = $_POST['ofile'];
$tcode = $_POST['tcode'];
$qaz = $_POST['qaz'];
$qaz = $_POST['qaz'];
$wsx = $_POST['wsx'];
$edc = $_POST['edc'];
$rfv = $_POST['rfv'];
$tgb = $_POST['tgb'];
$dt = $_POST['dt'];
if (md5($pschk) == $_SESSION['image_random_value'])
{
if (md5($qaz) == $_SESSION['filedtempc1'])
{
if (md5($wsx) == $_SESSION['filedtempc2'])
{
if (md5($edc) == $_SESSION['filedtempc3'])
{
if (md5($rfv) == $_SESSION['filedtempc4'])
{
if (md5($tgb) == $_SESSION['filedtempc5'])
{
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-Disposition: attachment; filename="$filen"");
header("Location: $subroot/$ofile?

pschk=$pschk&tcode=$tcode&qaz=$qaz&wsx=$wsx&edc=$edc&rfv=$rf

v&tgb=$tgb&td=$td");
} else {
header("Location: error.php");
exit;
}
}
}
}
}
}

it alway open in the "$ofile" name but not in the "$filen" name? why?

我也遇到這個問題,excel"突然"不能在yahoo mail真接開啟,也試過用其他電腦開啟,但也不能,為甚麼﹖你上述所說的php我也不明白是甚麼﹖可否幫幫我﹖

唔~找到這邊。
想請問,有沒有「反過來」,
就是不跳出「是否開啟或儲存這個檔案」的dialog,
直接就開啟excel / ppt / word 的方法呢?

pdf & jpg都可以,但是就office的不行。

謝謝!

發表您的評論

(必要)

(必要)