我將PDF文檔作爲LONGBLOB存儲在數據庫中。是否有將這些PDF嵌入網頁的解決方案?我找到了JavaScript的PDFObject,但它僅適用於存儲在文件夾中的PDF文件。或者,我可以將Blob中的PDF轉換成足夠長的時間來閱讀它嗎?將blob嵌入到html/php頁面
目前,我打開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);
?>
但是,它顯示了以下錯誤:
注意:試圖獲得的非財產object in/line 11 /Applications/XAMPP/xamppfiles/htdocs/D/PDFSHOW/pdfshow.php
注意:試圖讓非對象的財產/Applications/XAMPP/xamppfiles/htdocs/D/PDFSHOW/pdfshow.php上線12
注意:試圖讓非對象的屬性/應用程序/第13行的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
@Bram看看第一篇文章我添加了上傳部分 – Richard