做PHP图片上传与回显,上传没有问题,回显无报错但不显示图片

建表语句:

CREATE TABLE `image` (

`id` int(3) NOT NULL AUTO_INCREMENT,

`name` varchar(100) CHARACTER SET utf8 NOT NULL,

`pic` blob NOT NULL,

`type` varchar(50) CHARACTER SET utf8 NOT NULL,

`date` datetime NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1

conn.php

<?php

$conn = mysqli_connect('localhost', 'root', 'root', 'duoxuankuangceshi', '8080');

?>

upimg.php

<?php

include('./config.php');

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">

<head>

<meta http-equive="Content-Type" content=text/html charset=utf-8>

<title> </title>

</head>

<body>

<form method='post' action='./upload.php' enctype="multipart/form-data">

<input type="file" name="image" />

<input type="submit" name="submit" value="上传" />

</form>

<!-----------显示图片--------------------->

<table>

<?php

$ret = mysqli_query($conn,'select * from image order by id desc');

if ($ret) {

while ($row = mysqli_fetch_array($ret)) {

?>

<tr>

<td>

<img src="https://segmentfault.com/q/1010000024532410/image.php?id=<?php echo $row['id'];?>" width="170" height="150" border="0">

<div><?php echo $row['name'];?></div>

<?php echo $row['date'];?>

</td>

</tr>

<?php

}

}

?>

</table>

<!-----------/显示图片--------------------->

</body>

</html>

upload.php

<?php

include('./config.php');

if ($_POST['submit']) {

if ($_FILES['image']['size']) {

$names = $_FILES['image']['name'];

$arr = explode('.', $names);

$name = $arr[0]; //图片名称

$date = date('Y-m-d H:i:s'); //上传日期

$fp = fopen($_FILES['image']['tmp_name'], 'rb');

$type = $_FILES['image']['type'];

if (!$fp) {

showInfo('读取图片失败!');

} else {

$image = addslashes(fread($fp, filesize($_FILES['image']['tmp_name'])));

if ($image) {

$q = "insert into image (name, pic, type, date) values ('$name','$image','$type','$date')";

$result = mysqli_query($conn,$q);

if ($result) {

showInfo('上传成功!');

} else {

showInfo('上传失败!');

}

} else {

showInfo('请选择要上传的文件!');

}

}

} else {

showInfo('请选择要上传的文件!');

}

}

function showInfo($info)

{

echo "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";

echo "<meta http-equiv='refresh' content='1;url=upimg.php'>";

echo "</head>";

echo "<body>" . $info . "……</body>";

echo "</html>";

}

?>

image.php

<?php

include('./config.php');

$id = $_GET['id'];

$sql = "select * from image where id='$id'";

$result = mysqli_query($conn,$sql);

if (!$result)

die("读取图片失败!");

$num = mysqli_num_rows($result);

if ($num < 1)

die("暂无图片");

while ($row = mysqli_fetch_assoc($result)) {

$data = $row['pic'];

$type = $row['type'];

}

/*$data = mysqli_result($result, 0, 'pic');

$type = mysqli_result($result, 0, 'type');*/

mysqli_close($id);

Header("Content-type: $type");

echo $data;

?>

数据库:

效果图:

在做图片上传回显测试,现在图片已经上传成功了,但是图片的回显却没有显示出来,只显示框框,能获取图片对应的id,也没有报错,在地址栏中打开页面/image.php?id=1或2,两个打开页面都是只有页面正中间有个白色边框的小框,图片都不能显示,数据库里面的pic打开却有图片,请问该怎么让图片显示出来?

回答

你单独把图片请求的地址在浏览器里打开,你看输出的是啥,, 你的截图里,两个图片的请求,虽然状态码是200,但是 content-type 和 size 都不对,应该已经出错了.

mysqli_close($id);

参数传$id是错误的,改成

mysqli_close($conn);

以上是 做PHP图片上传与回显,上传没有问题,回显无报错但不显示图片 的全部内容, 来源链接: utcz.com/a/44512.html

回到顶部