When I download a blob image from my database its size is 1 KB. When I open the .png file itself, the image is not appearing. The image size appears in my MySQL database table. I couldn't figure out why it downloads an empty file from my database while the name and filetype are showing up correctly.
My table columns
Everything seems to be correct with name, filetype, and size.
File Upload.php
<?php
    require('config.php');
    session_start();
    if(isset($_POST['save']))
    {
        $target_dir = "upload/img/";
        $filename = explode('.', $_FILES['image']['name']);
        $ext = $filename[1];
        $imgname = time() . '.' . $ext;
        $target_file = $target_dir . $imgname;
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
        // Check if image file is a actual image or fake image
        $check = getimagesize($_FILES["image"]["tmp_name"]);
        if($check !== false) {
            $text = "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            $text = "File is not an image.";
            $uploadOk = 0;
        }
        // Check if file already exists
        if(file_exists($target_file)) {
            $text = "Sorry, file already exists.";
            $uploadOk = 0;
        }
        // Check file size
        if($_FILES["image"]["size"] > 2000000) {
            $text = "Sorry, your file is too large.";
            $uploadOk = 0;
        }
        // Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" &&
           $imageFileType != "gif" && $imageFileType != "bmp" ) {
            echo "Sorry, only JPG, JPEG, PNG, GIF & BMP files are allowed.";
            $uploadOk = 0;
        }
        // Check if $uploadOk is set to 0 by an error
        if($uploadOk == 0) {
            $_SESSION["error"] = $text;
            header("Location:index.php?id=$id"); /* Redirect browser */
            exit();
        // If everything is OK, try to upload the file
        }else {
            if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
                $path = $imgname;
                $finfo = new finfo();
                $array = explode('.', $_FILES['image']['name']);
                $extension = end($array);
                $filesize = $_FILES["image"]["size"];
                $conn->query("INSERT INTO images (image,name,filetype,size) VALUES ('$path','$path','$extension','$filesize')");
                $_SESSION["Success"] = 'Upload success';
                header("Location:index.php"); /* Redirect browser */
                exit();
            } else {
                $_SESSION["err"] = $text;
                header("Location:index.php"); /* Redirect browser */
                exit();
            }
        }
    }
?>
File Download.php
<?php
    // Include configuration file
    require_once 'config.php';
    $id = $_GET["id"];
    $sql = "select * from images where id=$id "; // 1
    $res = $conn->query($sql);
    while($row = $res->fetch_assoc())
    {
        $image = $row['image'];
        $name = $row['name'];
        $type = $row['filetype'];
        $size =  $row['size'];
    }
    header("Content-type: " . $type);
    header('Content-Disposition: attachment; filename="' . $name . '"');
    header("Content-Transfer-Encoding: binary");
    header('Expires: 0');
    header('Pragma: no-cache');
    header("Content-Length: " . $size);
    echo $image;
    exit();
?>


 
     
    