2015-09-25 45 views
0

我一直在尝试使用fopen()和readfile()的变体安全地提供PDF。无论我在Windows/OSX/iOS上,大多数方法都可以在Firefox和Chrome中正常工作,但方法可用于Internet Explorer或Safari。我尝试了各种各样的标题变化,基于别人的建议,但没有运气。当使用PHP fopen或读取文件提供PDF时,浏览器不兼容

这里是我的代码:

<?php 
session_start(); 
if ($_SESSION['user_is_logged_in'] === true || $_SESSION['admin_user_is_logged_in'] === true) { 

    $name = $_GET['name']; 
    $path = "www.mypath.com/$name"; 
    $fp = fopen($path, 'rb'); 

    header("Content-Type: application/pdf"); 
    header("Content-Length: " . filesize($path)); 
    //Experimented with this but all it does is force a download instead of opening in browser. 
    //header("Content-Disposition: attachment; filename=".$name); 

    readfile($path); 

    //alternately tried 
    //passthru($fp); 
} 
else { 
    echo "You do not have permission to view this file. Please <a href='../../index.php'>log in</a>."; 
} 

exit; 
?> 

如果我在标题中使用内容处置结果是在Safari或者IE下载的任何PDF是0KB而无法读取。没有内容处置IE和Safari只是挂在空白页面上。

回答

0

这些是一些建议我想和大家分享:

尝试使用localpaths即。 windows(c:\ folder \ resource.pdf)或linux(/var/www/project/resouce.pdf)。

打印您的$路径并检查是否生成正确的文件。

那就试试这一段代码,那是我用:

<?php 

header("Content-type: application/pdf"); 
header("Content-Disposition: attachment; filename={$this->filename}.pdf"); 

readfile($path); 
unlink($path);