2015-09-25 75 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);