我需要创建一个 pdf 文件fpdf http://www.fpdf.org库并将其保存在我的 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
该文件已正确显示,因此我认为该文件已正确生成,问题出在数据库中的存储中。
提前致谢。
实际上出于安全原因:
- 不使用
addslashes()
但使用mysql_real_escape_string()
instead
在这种特定情况下(因为它是二进制数据):
- Remove
stripslashes()
- Replace
addslashes()
by mysql_real_escape_string()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)