<?php
if (count($_FILES) > 0) {
    if (is_uploaded_file($_FILES['userImage']['tmp_name'])) {

        $imgData = addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
        $imageProperties = getimageSize($_FILES['userImage']['tmp_name']);

        $sql = "INSERT INTO qr(user_id,file_name ,QrCode)
                VALUES('".$_SESSION['id']."','{$imageProperties['mime']}', '{$imgData}')";
        $current_id = mysqli_query($db, $sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysqli_error($db));
        if (isset($current_id)) {
            header("Location: preview.php");
        }
    }
}
?>
    <form name="frmImage" enctype="multipart/form-data" action=""
        method="post" class="frmImageUpload">
        <label>Upload QrCode File:</label><br /> <input name="userImage"
            type="file" class="inputFile" /> <input type="submit"
            value="Submit" class="btnSubmit" />
    </form>



<?php
require_once '../php_action/database.php';
    if(isset($_GET['image_id'])) {
        $sql = "SELECT file_name , QrCode FROM qr WHERE id=" . $_GET['image_id'] . mysqli_error(($db)). mysqli_error(($db));
        $result = mysqli_query($db, $sql) or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysqli_error($db));
        $row = mysqli_fetch_array($result);
        header("Content-type: " . $row["file_name"]);
        echo $row["QrCode"];
    }
    mysqli_close($db);
?>

Recommended Answers

All 3 Replies

I'm trying to upload image but there is no image to show

  1. I recomend use prepared statement instead of directly put user input data to SQL query.
  2. Use SEND_LONG_DATA to store blob into database.

It's impossible for us to properly diagnose if anything is broken because you are including an external file php_action/database.php that we don't know the contents of.

That being said, it appears that the purpose of this code is to store the binary data that represents an image in the database. Are you able to tell if it was stored in there? So, two questions:

Firstly, can you confirm that is_uploaded_file() returns true? Secondly, can you confirm that the data is stored in the qr table?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.