gpt4 book ai didi

php - 在 MySQL blob 字段中保存使用 FPDF php 库创建的 PDF

转载 作者:可可西里 更新时间:2023-11-01 00:28:02 27 4
gpt4 key购买 nike

我需要使用 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

文件显示正确,所以我假设它是正确生成的,问题出在数据库中的存储上。

提前致谢。

最佳答案

实际上是出于安全原因:

  1. 不要使用addslashes(),而是使用mysql_real_escape_string()

在这种特定情况下(因为它是二进制数据):

  1. 删除 stripslashes()
  2. addslashes() 替换为 mysql_real_escape_string()

关于php - 在 MySQL blob 字段中保存使用 FPDF php 库创建的 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1307022/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com