php - 通过php连接到数据库后无法显示存储在MySQL数据库中的图像

标签 php mysql gd php-gd

我是一个完全的新手,刚刚开始使用 PHP 和 MySQL 数据库中的图像。这是问题所在:

图像已成功存储在 BLOB 字段中的数据库表中。我可以将其写入文件 (new.jpg) 并显示图像。现在这里是 twsiter - 我创建的用于在网页上显示图像的 imagedisplay.php 文件可以从文件夹目录中读取 new.jpg 文件。但是一旦我连接到数据库直接从数据库中获取图像数据(这样我就不必在代码中包含 new.jpg),它就会开始显示错误“图像包含错误...... "

上传图片使用的代码是

<?php
require_once 'login.php';  //contains the classes for connecting to databases
$dbh=new DB_Mysql;         //executing queries  
$func=new DB_Mysql_code_functions;

session_start();
if(isset($_SESSION['username']))
{
    echo<<<_END
    <form method="post" action="admin_social_activities.php" enctype="multipart/form-data">
    <table width="990">
    <tbody>
    <tr><td>Select an image file to be uploaded:</td></tr>
    <tr><td><input type="submit" value="UPLOAD" /></td></tr>
    </tbody>
    </table>
    </form>
_END;

    if(isset($_FILES['imagefile']['tmp_name']))
    {   
        $imagefile=$_FILES['imagefile']['tmp_name'];
        $image_size=$_FILES['imagefile']['size'];
        $image_name=addslashes($_FILES['imagefile']['name']);
        $image_data = addslashes(file_get_contents($imagefile));

        $image_array=getimagesize($imagefile);
        $image_type=$image_array['mime'];
        $image_height=$image_array[1];
        $image_width=$image_array[0];

        $maxfilesize=2000000;

        if($maxfilesize<$image_size)
        {
            echo "Please upload a smaller image. The size of the image is too   large.";
        }
        else
        {

            $query="INSERT INTO allery(image_name,image_type,image,image_size)  VALUES ('".$image_name."','".$image_type."','".$image_data."','".$image_size."')";
            $stmt=$dbh->execute($query);
            $lastimageid=mysql_insert_id();
            $query="select * from gallery where mage_id=".$lastimageid;
            $stmt=$dbh->execute($query);
            $row=$stmt->fetch_row();
            if(file_exists("new.jpg"))
            unlink("new.jpg");
            $handle=fopen("new.jpg",'wb');
            fwrite($handle,$row[3]);
            fclose($handle);

            echo "<p>You uploaded this image</p><img src='imagedisplay.php'     height=".($image_height/2)." width=".($image_width/2).">";
        }
    }
}

imagedisplay.php 文件的当前代码如下,它可以很好地显示图像:

<?php
header("Content-type: image/jpeg");
$image=imagecreatefromjpeg("new.jpg");
imagejpeg($image);
imagedestroy($image);
?>

一旦我在 imagedisplay.php 中包含连接查询,它就会停止显示图像

<?php
require_once 'login.php';
$dbh= new DB_Mysql();
$func=new DB_Mysql_code_functions;

header("Content-type: image/jpeg");
$image=imagecreatefromjpeg("new.jpg");
imagejpeg($image);
imagedestroy($image);
?>

我几天以来一直坚持这个......请帮忙..


好的...所以我改变了我的方法...我现在将 image_id 传递到查询字符串中,然后包括 imagedisplay.php 图像标签: 请注意,由于格式问题,我无法在此处编写代码的头部。头部部分是在 Dreamweaver 中预先格式化的标准 html 部分。

<body>
<div class="page shadow-round">


<div id="header">
<div id="logo">
<script type="text/javascript" src="../js/header.js"></script>
</div>
</div>
<div id="menu">
<script type="text/javascript" src="../js/navmenu.js"></script>
<script type="text/javascript">
</script>
</div>


<div class="content overflow" style="height:900px;">

<?php
require_once 'login.php';  //contains the classes for connecting to databases
$dbh=new DB_Mysql;         //executing queries  
$func=new DB_Mysql_code_functions;

session_start();
if(isset($_SESSION['username']))
{
    echo<<<_END
    <form method="post" action="admin_social_activities.php" enctype="multipart/form-data">
    <table width="990">
    <tbody>
    <tr><td>Select an image file to be uploaded:</td></tr>
    <tr><td><input type="submit" value="UPLOAD" /></td></tr>
    </tbody>
    </table>
    </form>
_END;

    if(isset($_FILES['imagefile']['tmp_name']))
    {   
        $imagefile=$_FILES['imagefile']['tmp_name'];
        $image_size=$_FILES['imagefile']['size'];
        $image_name=addslashes($_FILES['imagefile']['name']);
        $image_data = addslashes(file_get_contents($imagefile));

        $image_array=getimagesize($imagefile);
        $image_type=$image_array['mime'];
        $image_height=$image_array[1];
        $image_width=$image_array[0];

        $maxfilesize=2000000;

        if($maxfilesize<$image_size)
        {
            echo "Please upload a smaller image. The size of the image is too   large.";
        }
        else
        {

            $query="INSERT INTO gallery(image_name,image_type,image,image_size)  VALUES ('".$image_name."','".$image_type."','".$image_data."','".$image_size."')";
            $stmt=$dbh->execute($query);
            $lastimageid=mysql_insert_id();
        echo "<p>You uploaded this image</p>";
        echo "<img src='imagedisplay.php?imageid=".$lastimageid."' />";
        }
    }
}
else

echo "<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Your are <span class=\"red\"><b>not Authorized</b></span> to view this page. If you are the Admin, please login with your credentials again. <a href='login_page.php'>Click here to continue</a>";


?>
</div>
</body>
</html>

现在的问题是控件永远不会转到 imagedisplay.php 即。它无法完全引用 imagedisplay.php。

imagedisplay.php 的代码如下:

<?php
require_once 'login.php';
$dbh= new DB_Mysql();
$func=new DB_Mysql_code_functions;
$id=$_GET['imageid'];
$query="SELECT * FROM gallery where image_id=".$id;

$stmt=$dbh->execute($query);
$row=$stmt->fetch_row();
$imagedata=$row[3];
header("Content-type:image/jpeg");
echo $imagedata;
?>

我已经尝试了所有带引号的排列组合,尝试了 echo 语句以查看控制是否进入文件....但它没有...它仅保留在主文件中...我不明白原因。 ..请帮助...

最佳答案

您的查询有一些错误(“allery”、“mage_id”)。

$query="INSERT INTO allery(image_name,image_type,image,image_size)  VALUES ('".$image_name."','".$image_type."','".$image_data."','".$image_size."')";
...
$query="select * from gallery where mage_id=".$lastimageid;

如果您在 imagedisplay.php 中的代码中的任何地方有错误,它们将被输出并最终出现在图像数据中,从而破坏它。如果您删除 imagejpeg() 并且不发送图像标题,请查看 imagedisplay.php 输出的内容。这将为您提供必要的信息。

关于php - 通过php连接到数据库后无法显示存储在MySQL数据库中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9667666/

相关文章:

php - 如何将多维数组转化为json对象

连接五个表的 mySQL 查询

mysql - 如何从 Spring Boot Docker 服务连接远程 MySQL DB?

php - html 是否可以与 php 中动态生成的图像一起使用?

php - base64编码图像而不保存

php - 这个登录系统安全吗

php - 使用数组准备好的语句

php - 支票簿余额 PHP

perl - 使用 Cygwin 的 CPAN GD 模块安装失败

php - 如何在 ci 中使用动态添加和删除时内爆文本区域值?