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