【问题标题】:view pdf file in browser from mysql database从mysql数据库在浏览器中查看pdf文件
【发布时间】:2013-07-17 13:46:04
【问题描述】:

我正在尝试检索存储在 mysql 数据库中的 pdf 文件并将它们显示在浏览器中。单击查看按钮时,显示错误:failed to load pdf document

我的代码如下:

 $query=  mysql_query("SELECT uploaded_file FROM pdf_file where id='2'");
 header('Content-Description: File Transfer');
 header("Content-type: application/pdf");
 header('Content-disposition:inline; filename="'.$query.'"');
 @read($query);

在上面的代码中,当点击查看按钮时,传递了一个文件id。

【问题讨论】:

  • uploaded_file 文件名 - 即。 file.pdf - 还是实际文件?
  • 你真的把$query写在header('Content-disposition:inline; filename="'.$query.'"');吗?如果是这样,那么您一定不能获取该文件,因为 mysql_query 只为您提供资源,要获得结果,您必须使用 mysql_fetch_assoc($query) 或任何。
  • 这是实际文件
  • mysql 扩展已被贬值。建议使用mysqli或PDO。

标签: php mysql pdf phpmyadmin


【解决方案1】:

你没有使用结果集....

$result=  mysql_query("SELECT uploaded_file FROM pdf_file where id='2'");
$row=mysql_fetch_row($result);
$file = $row['uploaded_file'];
header("Content-type: application/pdf");
@read($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($file));
@read($file);

【讨论】:

  • 还有一件事 - mysql_query 已被弃用...见php.net/manual/en/function.mysql-query.php
  • 我尝试使用 mysql_fetch_row 但仍然显示加载 pdf 文件失败
  • 参数 $file 里有什么? $result 是否等于 true?如果不是您的查询不正确,可能是因为 id='2' 而不是 id=2 或其他原因
  • 我看到您写的结果确实返回了正确的文件名,在这种情况下,您的标题有问题 - 0 请参阅我的回复中的更正
  • 实际上我无法查看 pdf,因为当我退出时我的下载 ide 已打开,它被重定向到 pdf 格式,但在检索 PDF "localhost/pdfupload/viewpdf.php" 时仍然显示错误消息 Unexpected server response (0) .
【解决方案2】:

好的,为什么不将实际的 FILE 保存在目录中,并将位置保存到数据库中的文件中。然后只需从数据库中调用文件引用,如果您在浏览器中安装了 adobe,浏览器就会打开它。认为这可能是缓和然后将其保存为 blob。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    相关资源
    最近更新 更多