php - 用mysql和php打开pdf

标签 php mysql pdf

我将 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/

相关文章:

javascript - 我如何知道用户或单个 IP 地址在我网站的页面上花费了多长时间?

PHP:在多维数组中查找字符串或返回空数组键?

java - Hibernate 使用 hbm2ddl.auto=update 保留一些表,使用 hbm2ddl.auto=create 重新加载一些表

c# - 在 C# 中以编程方式读取 PDF

asp.net-mvc-2 - 从 .net mvc View 创建 pdf

php - 在 PHP 中从 MySQL 中提取主键

php - 如何在月份日期条件下从mysql表中获取确切数据

mysql - 使用相同的凭据同时从不同位置写入数据库

php - 为什么这个 MySQL 更新查询不起作用?

java - 使用Servlet在浏览器中显示Pdf文件