Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

php - 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.

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 enter image description here

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...