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只是掛在空白頁面上。