简体   繁体   中英

Displaying an image from database mysql, PHP

I have made a code where you can choose an image from your computer and upload it to the database. For some reason, after I have uploaded it(it is uploaded in the database correctly), the image that is showned isnt the image I have uploaded but it is a little image showing that it cant get the image from the database. Can someone help me?? Here is the code:

index.php:

<?php 
ob_start();
include_once('connect.php');
session_start();
?>
<html>
<head>
    <title>Upload an image</title>
</head>
<body>
    <form enctype="multipart/form-data" action="index.php" method="POST">
        <input type="file" name="image">
        <input type="submit" value="Upload">
    </form>
    <?php 
    //file properties
    $file = $_FILES['image']['tmp_name'];

    if(!isset($file)) {
        echo 'Please select an image.';
    }else{
       $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
       $image_name = addslashes($_FILES['image']['name']);
       $image_size = getimagesize($_FILES['image']['tmp_name']);

       if($image_size == false){
           echo 'That is not an image';
       }else{
           if (!$insert = mysqli_query($con,"INSERT INTO      uploading_image(name,image) VALUES('$image_name','$image')")){
               echo 'Problem uploading image';
           }else{
               $lastid = mysqli_insert_id();
               echo 'Image uploaded. <br>Your image:<br><img src="get.php? id='.$lastid.'">';
           }
       }  
    }
    ?>
</body>
</html>

$get.php:

<?php 
include_once('connect.php');

$id = addslashes($_REQUEST['id']);

$image = mysqli_query($con,"SELECT * FROM uploading_image WHERE id='$id'");
$find_image = mysqli_query($row = mysqli_fetch_array($image));
$image_db = $row['image'];

header("Content-type: image/png");

echo $image_db;
?>

check the datatype for the field image in the database. It should be blob or longblob. replace the following code

 $find_image = mysqli_query($row = mysqli_fetch_array($image));

with this.

$find_image = mysqli_query($row = mysqli_fetch_assoc($image));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM