2017-04-03 85 views
1

我试图在php应用程序中显示.pdf文件。它在我的本地开发环境中很好地工作,但是在生产中,相同的代码将pdf显示为乱码文本。pdf文件显示为文本/ html而不是应用程序/ pdf

这些是请求头我正在使用:

<?php 
$file = $_GET["f"]; 
$filename = 'contrato.pdf'; 
header('Content-type: application/pdf'); 
header('Content-Disposition: inline; filename="' .$filename. '"'); 
header('Content-Transfer-Encoding: binary'); 
header('Accept-Ranges: bytes'); 
readfile($file); 
?> 

这些都是根据铬生产响应头:

Connection:Keep-Alive 
Content-Encoding:gzip 
Content-Type:text/html 
Date:Mon, 03 Apr 2017 21:31:19 GMT 
Keep-Alive:timeout=15, max=189 
Server:Apache 
Transfer-Encoding:chunked 
Vary:Accept-Encoding 

在开发设置,响应头是这些:

Accept-Ranges:bytes 
Connection:Keep-Alive 
Content-Disposition:inline; filename="contrato.pdf" 
Content-Transfer-Encoding:binary 
Content-Type:application/pdf 
Date:Mon, 03 Apr 2017 21:33:44 GMT 
Keep-Alive:timeout=5, max=100 
Server:Apache/2.4.7 (Win32) PHP/5.5.8 
Transfer-Encoding:chunked 
X-Powered-By:PHP/5.5.8 

难道这是由于Apache的设置?

+2

只是为了检查:'%PDF-1.3':乱码输出的东西,如开始?您需要打开PHP中的错误报告。在发送标题之前查看是否有任何内容已经发送。 –

+0

可能有些代理或服务器正在改变它,或者您的请求改变标头被PHP拒绝。检查你的错误:http://stackoverflow.com/q/845021/1255289 – miken32

+0

是的,它从这样的事情开始。生产中出现错误,打开它们进行调试是个好主意。 –

回答

-2

我认为这是该行的错误:

header('Content-type: application/pdf'); 

注意小写字母牛逼

此行应该是这样的:

header('Content-Type: application/pdf'); 

也注意到,你是使用$ _GET [“f”] 读取文件是错误的,而是从$ filename变量读取文件。 更改最后一行:

readfile($filename); 

反正这里是你的代码的修正:

<?php 
$file = $_GET["f"]; 
$filename = 'abc.pdf'; 
header('Content-Type: application/pdf'); 
header('Content-Disposition: inline; filename="' .$file. '"'); 
header('Content-Transfer-Encoding: binary'); 
header('Accept-Ranges: bytes'); 
readfile($filename); 
?> 
+0

不,它们不区分大小写请参阅:RFC 2616 – nogad

+0

我也注意到并尝试使用大写T,它没有任何区别。关于文件名变量,我不认为它的问题,因为我可以访问该文件,问题是它显示为文本/ HTML。 –

+0

请注意,您正在尝试读取错误的文件 –