【问题标题】:Opening pdf with mysql and php用mysql和php打开pdf
【发布时间】:2016-05-06 07:31:58
【问题描述】:

我将 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 中收到了这条消息

无法加载 PDF 文档 重新加载

【问题讨论】:

  • $file 在您的代码中未定义!!
  • 这是我复制/粘贴源代码时的错误。我已经编辑了我的问题。
  • 确保 $file 上存储的路径正确,并尝试 $fileFolder 的绝对路径
  • echo $fileFolder.$file 并检查您的路径
  • 我没有得到文件名.. 当我echo $fileFolder.$file 我只有文件夹:uploads/

标签: php mysql pdf


【解决方案1】:

您可以使用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

【讨论】:

  • 感谢您的回答。现在我得到了正确的路径和文档,但仍然收到了这条消息。 Failed to load PDF document
  • echo $fileFolder.$file 我得到uploads/c970464e4da7eef4bb63c4c510b2ef36.pdf 这是正确的
  • 是的,这很有帮助。现在 PDF 已正确加载。感谢您的帮助!
【解决方案2】:

尝试以下方法:

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'];

    $myFilePath = $fileFolder . $file;

    if (file_exists($myFilePath)) {

        // the file exists, so open it
        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);

    } else {

        // the file doesn´t exits
        // handle the error if neccessary
        echo "the file doesn´t exist";

    }

}

【讨论】:

  • 感谢您的回答,但仍然无法加载,问题出在标题中。随着@Saty 对另一个问题的评论,我修复了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-13
  • 1970-01-01
  • 2013-06-23
  • 2020-01-21
  • 1970-01-01
  • 2011-01-01
相关资源
最近更新 更多