gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:14:05 25 4
gpt4 key购买 nike

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

$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”(而不是空,如果我什么都不插入)。当我尝试查询数据时,它是零字节。该代码适用于 PHP 5,但我还没有找到它不适用于 PHP 7 的任何原因。

最佳答案

切换到读取内容并将其传递到工作中。

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

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

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 - 如何使用 PHP 7 和 MySQL 将文件从 POST 插入到 longblob 列中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40053736/

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