如何使用PHP 7和MySQL将POST中的文件插入longblob列? [英] How can I insert file from POST into longblob column using PHP 7 and MySQL?

查看:70
本文介绍了如何使用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列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆