使用Blob将映像上传到MySQL数据库 [英] Uploading An Image To A MySQL Database Using A Blob
问题描述
首先要澄清一下,我知道有更好的方法可以做到这一点,但我决心进行试验.
Just to first clarify, I know there are better ways to do this, but I'm determined to experiment.
基本上,我正在尝试从表单发布中获取图像,然后将其发送到数据库 MEDIUMBLOB 字段中.
Basically, I'm trying to grab an image from a form post, then send that into a database MEDIUMBLOB field.
不幸的是,使用我当前的方法,数据库列中的最终结果始终为零字节. phpMyAdmin屏幕截图
Unfortunately, using my current method, the end result in the database column is always zero bytes. phpMyAdmin Screenshot
这是在表单上上传图片的代码:
Here is the code for the image upload on the form:
input type="file" name="_imagePost">
这是页面上的PHP代码,我正在使用MySQLi:
Here is the PHP code on the page, I'm using MySQLi :
if(isset($_POST['_imagePost']))
{
$_useImagePost = 1;
$_imagePost = file_get_contents($_FILES['_imagePost']);
// Open DB Connection
$_conn = databaseConnect();
$_stmt = $_conn->prepare("INSERT INTO Question (Question_Type, Question_Text, Question_Answer, Question_UseImage, Question_Image) VALUES (?, ?, ?, ?, ?)");
$_stmt->bind_param("sssib", $_typePost, $_textPost, $_answerPost, $_useImagePost, $_null);
$_stmt->send_long_data(4,$_imagePost);
$_stmt->execute();
// Close DB Connection
$_conn->close();
}
我不确定的是,当输入类型为file时,"isset($ _ POST ['_ imagePost'])"是否有效.
What I am unsure of is if "isset($_POST['_imagePost'])" works when the input type is file.
但是,我确定的是,当前设置根本无法使用.
What I am sure of, however, is that the current setup doesn't work at all.
推荐答案
您编写:
$_imagePost = file_get_contents($_FILES['_imagePost']);
正确的语法是:
$_imagePost = file_get_contents($_FILES['_imagePost']['tmp_name']);
$_FILES
是以下键的关联数组:
$_FILES
is an associative array whit following keys:
- [名称] => 原始文件名;
- [type] =>上传文件的模仿类型;
- [tmp_name] =>临时文件路径(存储上传文件的位置);
- [错误] =>错误;
- [大小] =>上传文件的大小.
- [name] => Original file name;
- [type] => mimetype of uploaded file;
- [tmp_name] => temporary filepath (where uploaded file is stored);
- [error] => error;
- [size] => size of uploaded file.
- 详细了解 $ _FILES
这篇关于使用Blob将映像上传到MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!