如何使用PHP检索/下载存储在MySQL数据库中的BLOB中的文件

时间:2022-09-25 23:16:31

The other day I recently wrote a program that takes a file that a user provided and inserts the actual file(driver) into a MySQL database BLOB. I did the insertion as follows:

前几天我最近写了一个程序,它接受用户提供的文件并将实际文件(驱动程序)插入MySQL数据库BLOB。我按如下方式插入:

$sql = "INSERT INTO Drivers (driver, filename, status, version, environments) VALUES(LOAD_FILE('$driver'), '$filename', $statusid, $driver_version, '$environments')";

As you can see above, the 'driver' field is the BLOB. I have the filename as well in a different field. So the question is, how can I easily get the BLOB and put it back into an actual file? I need to get this into a file so that I can upload it to a server via SCP. I've seen a few examples out there with content headers and such and they all look too complicated. It was easy to load with LOAD_FILE() and I didn't know if there was another simple option to get it back to an actual file? Sorry I don't have any code examples. I really don't know where to start.

如上所示,'driver'字段是BLOB。我在不同的领域也有文件名。所以问题是,如何轻松获取BLOB并将其放回实际文件中?我需要将其放入文件中,以便我可以通过SCP将其上传到服务器。我已经看到了一些带有内容标题等的示例,它们看起来都太复杂了。用LOAD_FILE()很容易加载,我不知道是否有另一个简单的选项让它回到实际的文件?对不起,我没有任何代码示例。我真的不知道从哪里开始。

1 个解决方案

#1


0  

Your sql table should have at least two extra fields:

你的sql表至少应该有两个额外的字段:

`mime_type` varchar(255) NOT NULL,
`size` int(10) unsigned NOT NULL,

where:

哪里:

  • mime_type is mime_content_type($path) and
  • mime_type是mime_content_type($ path)和
  • size is the file's size in bytes, filesize($path)
  • size是文件的大小(以字节为单位),文件大小($ path)

and PHP code should look like:

和PHP代码应如下所示:

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: {$file['mime_type']}");
header("Content-Transfer-Encoding: binary");
header("Content-Length: {$file['size']}");
header("Content-disposition: inline; filename= {$file['filename']}");

return $file['driver'];

The reasons for storing data as binary in SQL are dictated by business rules. For example, I store contracts that have to remain private.

在SQL中将数据存储为二进制的原因由业务规则决定。例如,我存储了必须保持私有的合同。

#1


0  

Your sql table should have at least two extra fields:

你的sql表至少应该有两个额外的字段:

`mime_type` varchar(255) NOT NULL,
`size` int(10) unsigned NOT NULL,

where:

哪里:

  • mime_type is mime_content_type($path) and
  • mime_type是mime_content_type($ path)和
  • size is the file's size in bytes, filesize($path)
  • size是文件的大小(以字节为单位),文件大小($ path)

and PHP code should look like:

和PHP代码应如下所示:

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: {$file['mime_type']}");
header("Content-Transfer-Encoding: binary");
header("Content-Length: {$file['size']}");
header("Content-disposition: inline; filename= {$file['filename']}");

return $file['driver'];

The reasons for storing data as binary in SQL are dictated by business rules. For example, I store contracts that have to remain private.

在SQL中将数据存储为二进制的原因由业务规则决定。例如,我存储了必须保持私有的合同。