【发布时间】:2014-03-31 07:33:48
【问题描述】:
我将 PDF 文档作为 LONGBLOB 存储在数据库中。是否有将这些 PDF 嵌入网页的解决方案?我找到了 JavaScript 的 PDFObject,但它只适用于存储在文件夹中的 PDF 文件。或者,我可以将 Blob 中的 PDF 转换成足够长的时间来阅读它吗?
目前,我使用以下脚本打开 PDF:
require_once 'init.php';
$sql = "SELECT data FROM dms_files WHERE id=42";
// the result of the query
$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
// set the header for the image
header( "Pragma: private");
header( "Cache-Control: max-age=0");
header("Content-type: application/pdf");
header('Content-Transfer-Encoding: binary');
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
echo $pdf;
它将 PDF 作为文件打开,但不会将其嵌入到页面中。
上传 PDF:
$docData = addslashes(file_get_contents($_FILES['document']['tmp_name']));
$docProperties = filesize($_FILES['document']['tmp_name']);
$sqldoc = "INSERT INTO dms_files(mime ,data) VALUES('{$docProperties}', '{$docData}')";
$current_id = mysql_query($sqldoc) or die("<b>Error:</b> Problem on Doc. Insert<br/>" . mysql_error());
$docId = mysql_insert_id();
我试过用这个:
<?php
require_once 'init.php';
$sql = "SELECT * FROM dms_files WHERE id = 42 ";
// the result of the query
$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
$row = mysql_fetch_row($result);
$basedir = 'LIB/';
$pdfname = strtolower(preg_replace('/([^\w\d\-_]+)/', '-', $row->nazov_rd));
$filename = $basedir . $pdfname . '_' . $row->id. '.pdf';
$file_content = base64_decode($row->data);
file_put_contents('filename.pdf', $row->data);
?>
但是,它显示了以下错误:
注意:尝试在第 11 行的 /Applications/XAMPP/xamppfiles/htdocs/D/PDFSHOW/pdfshow.php 中获取非对象的属性
注意:试图在第 12 行的 /Applications/XAMPP/xamppfiles/htdocs/D/PDFSHOW/pdfshow.php 中获取非对象的属性
注意:尝试在第 13 行的 /Applications/XAMPP/xamppfiles/htdocs/D/PDFSHOW/pdfshow.php 中获取非对象的属性
注意:试图在第 14 行的 /Applications/XAMPP/xamppfiles/htdocs/D/PDFSHOW/pdfshow.php 中获取非对象的属性
怎么了?当我打开 filename.pdf 时,预览显示“文件为空,无法打开”
我能够解决它;这是丢失的:
while($row = mysql_fetch_object($rs)){
【问题讨论】:
-
你能发布你现在使用的完整代码吗?
-
@Bram 看看第一篇文章我添加了上传部分