Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
2.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share

548k questions

547k answers

4 comments

56.5k users

...