2010-06-07 54 views
1
header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename='.basename($file)); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
ob_clean(); 
flush(); 
readfile($file); 

我下载的文件却无法打开文件?为什么?无法打开用php代码下载的文件

+1

只为上下文的人回答这样的:代码示例提问引述是从文档上php.net为[ReadFile的](HTTP:/ /uk2.php.net/readfile)功能 – 2010-06-07 02:59:20

+0

无法在本地机器上的...中打开文件? – alex 2010-06-07 02:59:29

+0

使用本地主机 在本地主机的webroot打开此文件,它打开, 当我下载并打开这个文件,但不能 – Chameron 2010-06-07 09:13:54

回答

3

尝试下面的代码(未测试):

header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename='.basename($file)); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
@ob_end_clean(); 
readfile($file); 
exit(); 
+3

是不是输出缓冲的东西之一完成允许头之前通过任何内容? – 2010-06-07 03:55:29

+1

@Arda Xi正确,ob_clean不会影响标题。 – Charles 2010-06-07 04:02:11

+0

我纠正了(这就是为什么我说“我认为”而不是做出明确的断言)。感谢那些家伙的接班人 - 我每天都会学到新的东西。 – 2010-06-07 04:49:22