2010-09-17 109 views
5

我正在使用PHP從數據庫查詢創建CSV文件。 我運行查詢,設置標題,並在Firefox中加載頁面,文件提示下載並在Excel中打開,就像它應該的那樣。 當我在IE中嘗試它時,出現錯誤,說Internet Explorer cannot download ReportPrint.php from www.website.com.
Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later.

不知道可以做什麼來解決這個問題。使用PHP創建和下載CSV

header('Content-Description: File Transfer'); 
header("Content-Type: application/csv") ; 
header("Content-Disposition: attachment; filename=Report.csv"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 

echo "record1,record2,record3\n"; 
+0

備註:「Expires」標題期望*日期* – 2010-09-17 19:55:09

回答

1

卸下Pragma: no-cache報頭。此標頭可防止IE下載文件。

+0

嗯,這可能已經完成了...刪除no-cache並沒有解決它,但我發現另一個地方是我從我的網站下載PDF文件,並注意到我沒有的幾行。我認爲這是解決它的標題(「Pragma:public」)。 – AndyD273 2010-09-17 19:51:19

+0

看來,刪除頭是你的實際修復。但是,停止添加它是不夠的:如果您使用會話,PHP可以自行添加它。 – 2010-09-18 10:47:56

0

檢查IE的安全設置,可能是它配置爲不下載CSV文件。

+0

我檢查了設置,沒有發現任何問題。然後我去了另一個網站,並能夠下載一個動態創建的CSV。最後,我創建了一個靜態CSV'test.csv',它下載得很好,所以我相當確定它與頭文件有關。 – AndyD273 2010-09-17 19:36:04

+0

此外,我的一位同事嘗試過,也未能在新版本的Windows 7上下載生成的CSV文件。 – AndyD273 2010-09-17 19:44:26

-2

變化

header("Content-Disposition: attachment; filename=Report.csv"); 

header("Content-Disposition: attachment;filename=Report.csv"); 
3

Internet Explorer在無法緩存文件時往往會顯示這些錯誤消息,而您似乎試圖避免緩存。嘗試,而不是像這樣:

<?php 

$seconds = 30; 

header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$seconds) . ' GMT'); 
header('Cache-Control: max-age=' . $seconds . ', s-maxage=' . $seconds . ', must-revalidate, proxy-revalidate'); 

session_cache_limiter(FALSE); // Disable session_start() caching headers 
if(session_id()){ 
    // Remove Pragma: no-cache generated by session_start() 
    if(function_exists('header_remove')){ 
     header_remove('Pragma'); 
    }else{ 
     header('Pragma:'); 
    } 
} 

?> 

調整$seconds根據自己的喜好,但不要將其設置爲零。

它還有助於使用mod_rewrite使IE認爲,下載的是一個靜態文件,例如,http://example.com/Report.csv

請報到,並告訴它是否適合你。

+0

一旦我把'Pragma:Public'開始工作。更改過期似乎沒有任何區別...... Dunno – AndyD273 2010-09-17 21:08:16

+0

如果我沒有錯,Pragma的唯一有效值是'no-cache'。將其設置爲其他任何內容只會讓瀏覽器忽略它(因爲它不是已知的值),並且與刪除標頭相同。無論如何,我很高興它解決了你的問題。 – 2010-09-18 10:44:38

+0

我有同樣的問題; Expires頭沒有任何影響,但只需清除Cache-Control和Pragma頭就可以了。 +1 – iandisme 2012-06-21 20:39:24