我将 pdf 存储在数据库表中的文件系统和路径中。我现在想根据 ID 在浏览器中打开相应的 PDF 文档。如何打开和阅读 PDF? 到目前为止我所拥有的是这个
require_once("database.php");
if(isset($_GET['upload_id']) && is_numeric($_GET['upload_id']))
{
$fileFolder='uploads/';
$sql = "SELECT file FROM documents WHERE upload_id = :id";
$result = $pdo->prepare($sql);
$result->bindParam(":id", $_GET['upload_id']);
$result->execute();
$resArray = $result->fetchAll();
$file = $resArray['file'];
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: application/pdf');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($fileFolder.$file));
@read($fileFolder.$file);
}
当我点击按钮并尝试打开 pdf 时,我在 Chrome 中收到了这条消息
Failed to load PDF document RELOAD
最佳答案
您可以使用 fetch(PDO::FETCH_ASSOC);
获取文件名,它将下一行作为由 column name
索引的数组返回
$sql = "SELECT file FROM documents WHERE upload_id = :id";
$result = $pdo->prepare($sql);
$result->bindParam(":id", $_GET['upload_id']);
$result->execute();
$resArray = $result->fetch(PDO::FETCH_ASSOC);
$file = $resArray['file'];// get your file name
通过使用fetchAll
传递 PDO::FETCH_COLUMN, 0
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);
$file = $resArray[0];// get your file name
关于php - 用mysql和php打开pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37066798/