我是一个完全的新手,刚刚开始使用 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/> 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 语句以查看控制是否进入文件....但它没有...它仅保留在主文件中...我不明白原因...请帮助...