Uploading An Image To A MySQL Database Using A Blob

Deadly 提交于 2019-11-28 12:14:57

问题


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.


回答1:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!