获得"The Image cannot be displayed because it contains an error"



i是我的数据库上的图像数据作为中间机,并在用户选择图片相应的ID时尝试将其作为图像检索。在任何人跳下我的喉咙之前,是的,我知道这是一个存储在文件服务器上并以这种方式检索的流程,但是这是对班级的,这就是教授想要的。

 <HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>BLOB Data Type Tutorial</title>
</head>
<body>
<form action="images.php" method="POST" enctype="multipart/form-data">
    Select Picture: <input type="pictureID" name="pictureID"> <input type="submit" value="submit" name="submit"> 
</form>
<?php
    $link = mysqli_connect('127.0.0.1:3306','user','');
    $link;
    mysqli_select_db($link, "media");
    $sql = "SELECT * FROM images";
    $result = mysqli_query($link,$sql);
    echo "<table>";
    echo "<tr><th>Picture</th><th>Size KB</th><th>Title</th></tr>";
    if($result === FALSE){
        echo "failed";
        die(mysql_error());
    }
    while($row = mysqli_fetch_array($result)) {
        $Picture = $row['Picture'];
        $Size = $row['Size KB'];
        $Title = $row['Title'];
        echo "<tr><td style ='width: 50px;'>".$Picture."</td><td style='width: 60px;'>".$Size."</td><td>".$Title."</td></tr>";
    }
    echo "</table>";
    if(isset($_POST['pictureID']))
    {
   $imgId = $_POST['pictureID'];
   echo $imgId;
   if (!empty($imgId)) {
   $sqliCommand = "SELECT Picture FROM images WHERE Picture = $imgId";
   $result = mysqli_query($link, $sqliCommand);
   $row = mysqli_fetch_assoc($result);
   header("Content-type: image/jpeg");
   echo $row['Image'];
    }
    }
    else
    {
        echo "^ Please select a picture! ^";
    }
?>

<br>
 <h4> 
     <a href="./index.html">Main Menu</a> <br>
</h4>
</body>
</HTML>

任何提示/帮助都将不胜感激!

这是由于页面顶部的HTML代码而引起的。当执行header("Content-type: image/jpeg");时,Chrome将整个页面的内容解释为包括HTML的图像数据。

要修复此此操作时,将ID输出在脚本的顶部传递时将图像输出放置,并在输出图像时立即退出脚本,否则代码也将输出。

应该这样做:

<?php
    //this should be at top of php file, above html code
    $link = mysqli_connect('127.0.0.1:3306','user','');    
    mysqli_select_db($link, "media");
    if(isset($_POST['pictureID']))
    {
        $imgId = $_POST['pictureID'];
        //echo $imgId; nothing else expect image should be echoed
        if (!empty($imgId)) {
            $sqliCommand = "SELECT Image FROM images WHERE Picture = $imgId";// select Image here so not select picture as you did earlier 
            $result = mysqli_query($link, $sqliCommand);
            $row = mysqli_fetch_assoc($result);
            mysqli_close($link);//close mysqli as script gonna exit after echoing image.
            header("Content-type: image/jpeg");
            echo $row['Image'];
            exit();// this is important
        }
    }  
?>
<html>
    <body>  
    <!-- rest of the code -->

阅读我在代码中写的评论。

如果将标题设置为图像类型,则一定没有任何期望在页面上的图像二进制代码。

如果问题持续存在,请评论标题行(这将使页面以文本形式出现),并查看页面中是否有其他任何东西以二进制形式的图像数据。

相关内容

最新更新