[英]Cannot display image stored in MySQL database after connecting to database through php
我是一个新手,刚刚开始使用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 image标签:请注意,由于某些格式问题,我无法在此处编写代码的开头部分。 头部分是在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/> 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输出的内容。 这将为您提供必要的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.