我有阅读pdf的php url问题

时间:2016-12-28 07:19:55

标签: php codeigniter-3

现在我正在开发codeigniter网站。 我有一个问题。 为了阅读和打开PDF到Web浏览器。

header("Content-type:application/pdf");         
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Cache-Control: max-age=0'); //no cache          
header("Content-Disposition:attachment;filename=\"".$file."\"");
readfile($filePath);

上面的代码$ file是filename,$ filePath是pdf文件路径。 当它在本地服务器上运行时,$ filePath的值是“http://localhost/ .pdf”,它运行良好。 但是当在托管服务器上运行时,此值为“http:// .com / ***。pdf” 并没有运行。 我们无法打开pdf格式错误。 文件内容不包括readed。 我知道这是URL问题的原因。 但我没有问题!

1 个答案:

答案 0 :(得分:0)

如果你想在浏览器中显示文件而不是下载文件,那么你的Content-Disposition应该是inline,但我想如果你能让它发挥作用并不重要。

如果PHP中的allow_url_fopen配置设置为Off,则无法从PHP脚本中读取URL文件。

无论如何,你的代码看起来应该是这样的。

<?php

$file = "lesson2.pdf";
$filePath = "http://kmmc.in/wp-content/uploads/2014/01/lesson2.pdf";

header("Content-Type: application/pdf");
header("Cache-Control: max-age=0");
header("Content-Disposition: inline; filename=\"" . $file . "\"");
readfile($filePath);