<?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);
?>

Dani AI

Generated

A few likely root causes, pulled from the code and the replies by and :

  • The retrieval SQL looks malformed (there are accidental concatenations of error output into the query) which will break the SELECT and return nothing.
  • The insert may be using an empty session value if session_start() was never called.
  • Binary data should not be escaped with addslashes() — use prepared statements (or store files on disk).
  • Make sure the preview script sends no HTML or whitespace before header() and that the correct MIME is returned.

Quick checklist to reproduce and fix the problem

  1. Turn on errors while debugging: error_reporting(E_ALL); ini_set('display_errors', 1);.
  2. Dump the upload: var_dump($_FILES); and confirm $_FILES['userImage']['error'] === UPLOAD_ERR_OK (see PHP upload error codes).
  3. Confirm session_start() runs before you read $_SESSION['id'].
  4. Inspect the SQL string before executing it (log it or echo it while debugging) — remove any mysqli_error() concatenation that was accidentally appended. Cast $_GET['image_id'] to int or use a prepared statement.
  5. Ensure the DB column is a suitable BLOB type (BLOB vs MEDIUMBLOB vs LONGBLOB) for the file size.
  6. When sending the image, send the correct Content-Type, output only the raw bytes and then exit.

Example (robust retrieval using a prepared statement):

<?php
require_once '../php_action/database.php';
if (! empty($_GET['image_id'])) {
    $id = (int) $_GET['image_id'];
    $stmt = $db->prepare('SELECT mime_type, image_data FROM qr WHERE id = ?');
    $stmt->bind_param('i', $id);
    $stmt->execute();
    $stmt->bind_result($mime, $data);
    if ($stmt->fetch()) {
        header('Content-Type: ' . $mime);
        echo $data;
    } else {
        http_response_code(404);
    }
    $stmt->close();
    exit;
}

If you prefer storing files on disk (simpler and often faster), use move_uploaded_file() and save the path in the DB. See the PHP docs on file uploads, move_uploaded_file() and session_start() for details. Useful reading: file upload errors, move_uploaded_file, session_start, and mysqli prepared statements.

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.