如何使用 PHP 7 和 MySQL 将文件从 POST 插入 longblob 列?

2024-08-09php开发问题
3

本文介绍了如何使用 PHP 7 和 MySQL 将文件从 POST 插入 longblob 列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用 PHP 7 和 MariaDB 5.5.47(在 CentOS 7 上)并且以下代码成功执行,没有错误:

I'm using PHP 7 and MariaDB 5.5.47 (on CentOS 7) and the following code successfully executes, with no errors:

$id = 3;
$tmp_name = $_FILES['file']['tmp_name'];
$fp = fopen($tmp_name, 'rb');

$statement = $db->prepare("INSERT INTO Foo (id, data) VALUES (?, ?)");
$statement->bindParam(1, $id);
$statement->bindParam(2, $fp, PDO::PARAM_LOB);
$statement->execute();
$statement->closeCursor();
close($fp);

使用 MySQL WorkBench 它显示数据"列是BLOB"(与 null 相反,如果我什么都不插入).当我尝试查询数据时,它是零字节.这段代码适用于 PHP 5,但我没有找到任何原因说明它不能适用于 PHP 7.

Using MySQL WorkBench it shows the "data" column is "BLOB" (as opposed to null, in the event I insert nothing). When I try to query the data, it's zero bytes. This code worked with PHP 5, but I haven't found any reason why it wouldn't work with PHP 7.

推荐答案

切换到阅读内容并将其传入工作.

Switching to reading the content and passing that in worked.

$fp = fopen($tmp_name, 'rb');
$content = fread($fp, filesize($tmp_name));
// ...
$statement->bindParam(2, $content, PDO::PARAM_LOB, $size);

但是,我在上传大于 20mb 的文件时遇到了内存错误:

However, I run into errors about memory when uploading a file that's > 20mb:

致命错误:允许的内存大小为 134217728 字节已用尽(试图分配 81606320 字节)在 /var/www/app/API/docs.php在线 498

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 81606320 bytes) in /var/www/app/API/docs.php on line 498

这篇关于如何使用 PHP 7 和 MySQL 将文件从 POST 插入 longblob 列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17