【发布时间】:2016-02-11 08:57:29
【问题描述】:
我需要将 pdf 文件保存到 BLOB 中(数据库是 MsSql 2012,但我也需要它在 2008 上工作)。
这是我迄今为止尝试过的汇编:
(文件where I save pdf into database)
function preparePDF2String($filepath)
{
$handle = @fopen($filepath, 'rb');
if ($handle)
{
$content = @fread($handle, filesize($filepath));
$content = bin2hex($content);
@fclose($handle);
return "0x".$content;
}
}
$out = preparePDF2String('pdf/pdf.pdf');
$q_blob = "INSERT INTO HISTORIA_ARCHIWUM_test(PDFName, PDFData) VALUES('First test pdf', $out) ";
$r_blob = mssql_query($q_blob,$polacz);
Results(好吧,我想)
(现在是php文件where I try to output the pdf)
// header('Content-type: application/pdf');
// readfile('pdf/pdf.pdf'); - this works just fine, so pdf is ok
$q_blob = "select top 1 * from HISTORIA_ARCHIWUM_test";
$r_blob = mssql_query($q_blob,$polacz);
$w_blob = mssql_fetch_array($r_blob);
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
echo $w_blob['PDFData'];
//echo hex2bin ($w_blob['PDFData']); - I also tried this - same problem
问题:当我尝试在浏览器中输出 pdf 时出现错误(此 pdf 格式不受支持)。它也不会在 Adobe 阅读器中打开。我的猜测是我输出错误,但也许是我保存它的方式?
【问题讨论】:
-
为什么不将 PDF 保存在文件系统中的数据库之外,将其路径链接保存在数据库中并让 PHP 使用该链接(很像图像)?您避免将二进制内容转换为字符串。
-
这是我的客户要求。他需要易于复制的大型 pdf 存储库(自动备份 - 一个大数据库文件)
标签: php sql-server pdf blob