Just to first clarify, I know there are better ways to do this, but I'm determined to experiment.
Basically, I'm trying to grab an image from a form post, then send that into a database MEDIUMBLOB field.
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">
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();
}
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.
You write:
$_imagePost = file_get_contents($_FILES['_imagePost']);
The correct syntax is:
$_imagePost = file_get_contents($_FILES['_imagePost']['tmp_name']);
$_FILES is an associative array whit following keys:
- [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.
- See more about $_FILES
来源:https://stackoverflow.com/questions/35417567/uploading-an-image-to-a-mysql-database-using-a-blob