2010-09-17 72 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