将使用 FPDF php 库创建的 PDF 保存在 MySQL blob 字段中
我需要使用 fpdf 库创建一个 pdf 文件,并将其保存在我的 MySQL 数据库中的 blob 字段中。 问题是,当我尝试从 blob 字段检索文件并将其发送到浏览器进行下载时,下载的文件已损坏并且无法正确显示。
如果我立即将其发送到浏览器而不将其存储在数据库中,则相同的 pdf 文件会正确显示,因此在插入数据库时似乎某些数据会损坏。
我的代码是这样的:
$pdf = new MyPDF(); //class that extends FPDF and create te pdf file
$content = $pdf->Output("", "S"); //return the pdf file content as string
$sql = "insert into mytable(myblobfield) values('".addslashes($content)."')";
mysql_query($sql);
存储pdf,并且像这样:
$sql = "select myblobfield from mytable where id = '1'";
$result = mysql_query($sql);
$rs = mysql_fetch_assoc($result);
$content = stripslashes($rs['myblobfield']);
header('Content-Type: application/pdf');
header("Content-Length: ".strlen(content));
header('Content-Disposition: attachment; filename=myfile.pdf');
print $content;
将其发送到浏览器进行下载。 我究竟做错了什么?
如果我将代码更改为:
$pdf = new MyPDF();
$pdf->Output(); //send the pdf to the browser
文件正确显示,所以我假设该文件已正确生成,问题出在数据库中的存储中。
提前致谢。
I need to create a pdf file with the fpdf library and save it in a blob field in my MySQL database.
The problem is, when I try to retrieve the file from the blob field and send it to the browser for the download, the donwloaded file is corrupted and does not display correctly.
The same pdf file is correctly displayed if I send it immediately to the browser without storing it in the db, so it seems some of the data gets corrupted when is inserted in the db.
My code is something like this:
$pdf = new MyPDF(); //class that extends FPDF and create te pdf file
$content = $pdf->Output("", "S"); //return the pdf file content as string
$sql = "insert into mytable(myblobfield) values('".addslashes($content)."')";
mysql_query($sql);
to store the pdf, and like this:
$sql = "select myblobfield from mytable where id = '1'";
$result = mysql_query($sql);
$rs = mysql_fetch_assoc($result);
$content = stripslashes($rs['myblobfield']);
header('Content-Type: application/pdf');
header("Content-Length: ".strlen(content));
header('Content-Disposition: attachment; filename=myfile.pdf');
print $content;
to send it to the browser for downloading.
What am I doing wrong?
If I change my code to:
$pdf = new MyPDF();
$pdf->Output(); //send the pdf to the browser
the file is correctly displayed, so I assume that is correctly generated and the problem is in the storing in the db.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
实际上出于安全原因:
addslashes()
而是使用mysql_real_escape_string()
代替在这种特定情况下(因为它是二进制数据):
stripslashes()
addslashes()
替换为mysql_real_escape_string()
Actually for security reasons:
addslashes()
but usemysql_real_escape_string()
insteadIn this specific case (as it is binary data):
stripslashes()
addslashes()
bymysql_real_escape_string()
您不应该删除任何(反)斜杠。添加它们时,它们将用作查询中的转义字符,并且不会出现在字段中。
尝试将其更改
为:
You shouldn't strip any (back)slashes. When you add them, they are used as escape characters in the query and do not appear in the field.
Try changing this
into this:
比较字符串中的差异以帮助调试问题。
和
Compare the differences in the strings to help debug the problem.
and
替换
为
对我有用。
Replacing
with
worked for me.
解决了!
我也遇到了同样的问题,并正在尝试上述想法。解决这个问题的原因是我的 MySQL 数据库将 PDF 存储为
blob
,而不是mediumblob
。 Blob 字段限制为 64K,因此截断了 PDF 的其余部分,其中包括 EOF 等有价值的数据(因此出现了令人讨厌的 Adobe 错误)。要进行确认,请下载其提供的 PDF 文件并查看文件大小。如果正好是 64K,您需要通过将该字段设置为
mediumblob
来在数据库中腾出更多空间。感谢大家的帮助!
顺便说一句,我听从了 Toto 的建议,改用
mysql_real_escape_string()
,同时:stripslashes()
addslashes()
替换为mysql_real_escape_string()
通过对原始海报代码的这些更改,它起作用了!
SOLVED IT!
I too was having the same problem and was trying the above mentioned ideas. What fixed this for me was that my MySQL database was storing the PDF as a
blob
instead ofmediumblob
. A blob field is limited to 64K and was thereby truncating the rest of my PDF which included the valuable data including EOF (hence the nasty Adobe Error).To confirm, download the PDF file it serves up instead and look at the file size. If it's exactly 64K you'll need to make more room in the DB by setting the field to
mediumblob
.Thanks everyone for the help!
By the way, I followed Toto's advice and used
mysql_real_escape_string()
instead, along with:stripslashes()
addslashes()
bymysql_real_escape_string()
With those changes to the original poster's code it worked!
我最近正在解决这个问题,在我们给出的测试解决方案之后给出答案很重要。然后,我按如下方式解决了这个问题。 为了保存生成的fpdf,我使用了这个代码..:
从我的数据库中读取(从数据库检索)对我来说最好使用pdo代码:
这些代码工作得很好。我希望这个贡献可以帮助其他人摆脱这个问题带来的头痛。此致。
I was recently working with this problem and it is important to give answer after test solutions given by us. Then, I solved this problem as follow. To save generated fpdf i have used this code..:
To read (retrieve from database) from my database was preferable to me use pdo code:
Those codes worked perfectly. I hope this contribution helps others to turn off headache with this problem. Best Regards.
我遇到了与上面提到的同样的问题,更仔细地查看 phpmyadmin,我注意到 pdf 的插入是以十六进制完成的,而 fpdf 返回二进制,我的情况的解决方案是使用 bin2hex():
然后你可以插入sql查询中的这个$_hex不使用addslashes()或任何东西...(当然,我认为它必须插入到中型或长型blob中)
I encountered the same problem as mentioned above, looking more closely at phpmyadmin, I noticed that the insert of a pdf is done in hex, while fpdf returns binary, the solution in my case was to use bin2hex():
And you can then insert this $_hex in a sql query without using addslashes() or anything... (of course it has to be inserted in a medium or long blob i think)