简体   繁体   中英

Error in displaying an image in php mysql

Hey I am facing a problem in displaying images in php. The images are being stored in a table 'images' in mysql. There is another table 'restaurant' which needs to fetch those images and display respective images according to the restid. However, it is facing a problem in fetching the images and not displaying them. Please help! This is imageupload.php:

<?php
require 'connect.inc.php';
?>
<html>
<head>
<title>Uploading image</title>
</head>
<body>
<?php
echo "<form action='imageupload.php' method='POST' enctype='multipart/form-data'>

Upload: <input type='file' name='image'><input type='submit' value='Upload' >
</form>";

if(isset($_FILES['image']['tmp_name']))

{
    $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's not an image";
    else
    {
        $query = "INSERT INTO images VALUES ('','$image_name','$image','22')";
        $result = mysqli_query($con, $query);

        if(!$result)
        {
            echo "Problem uploading";

        }
        else
        {
            echo "Image uploaded ";
            $query2 = "SELECT * FROM images WHERE restid = '22'";
            $result2 = mysqli_query($con,$query2);
            while($info = mysqli_fetch_array($result2))
            {
                header("Content-type: image/jpeg");
                echo $info['image'];
            }
        }
    }
}
else
{
    "Please upload a file";
}
 ?>
</body></html>

This is getimage.php (It fetches the image and displays it):

<?php
require 'connect.inc.php';
$id = $_REQUEST['id'];
$image = "SELECT * FROM images WHERE imgid = $id" ;
$image = mysqli_query($con, $image);
$image = mysqli_fetch_assoc($image);
$image = $image['image'];

header("Content-type: image/jpeg");
echo $image;
?>

connect.inc.php is a file to connect to the database. I referred to other links but did not get any solid help. Please provide help.

Storing image in mysql should work. Check that you don`t have any syntax errors. Temporary remove Content-type header to see that image file gets printed (as gibberish string). Also check that mysql field you store image is BLOB type. Post if you have any error there.

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