所以我试图显示用户在主页上传的图像。但是,当其他信息(描述,标题,用户名,日期变量)显示正常时,图像将不会出现由于某种原因。那么有人可以向我指出阻止图像出现的错误吗?图像存储在数据库中,服务器上的文件夹就可以了。
以下是我的home / index.php页面上的代码:
<!doctype html>
<html>
<head>
<?php
include('header.php');
?>
<title>Splindr</title>
<link type="text/css" rel="stylesheet" href="index.css">
</head>
<body>
<div id="contentWrapper">
<?php
$conn = mysqli_connect("localhost","root","") or die ("No SQLI");
mysqli_select_db($conn, "sample") or die ("No DB");
$sqli = "SELECT * FROM `photos` WHERE `username` = 'ralston3'";
$result = mysqli_query($conn, $sqli) or die ("No query");
while($row = mysqli_fetch_assoc($result)) {
$username = $row['username'];
$title = $row['title'];
$description = $row['description'];
$image_name = $row['image_name'];
$image_id = $row['image_id'];
$date = $row['post_date'];
}
$image_id = $image_id;
echo "<div id='photo'><div id='image'><img src='$image_id'><div id='info_header'>$title   by   $username   on   $date </div><div id='imageInfo'>$description</div></div></div>";
?>
</div>
</body>
</html>
以下是我的posts_check.php页面的代码,当用户上传帖子时会发送帖子:
<?php
$conn = mysqli_connect("localhost","root","") or die ("No SQLI");
mysqli_select_db($conn, "sample") or die ("No DB");
if (isset($_FILES['image']) && ($_FILES['image']['size'] < 2097152) && (in_array($_FILES['image']['type'], array('image/jpeg', 'image/png', 'image/jpg', 'image/gif')))) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rand_dir_name = substr(str_shuffle($chars), 0, 15);
mkdir("photos/$rand_dir_name");
if (file_exists("photos/$rand_dir_name/".$_FILES["image"]["name"]))
{
echo $_FILES["image"]["name"]." Already exists";
}
else
{
move_uploaded_file($_FILES["image"]["tmp_name"],"photos/$rand_dir_name/".$_FILES["image"]["name"]);
//echo "Uploaded and stored in: phoyoss/$rand_dir_name/".@$_FILES["image"]["name"];
$username = 'ralston3';
$title = $_POST['title'];
$description = $_POST['description'];
$image_name = $_FILES['image']['name'];
$image_id_before_md5 = "$rand_dir_name/$image_name";
$image_id = md5($image_id_before_md5);
$date = date('Y-m-d H:i:s');
$sqli = "INSERT INTO `photos` (username, title, description, image_name, image_id, post_date) VALUES ('ralston3','$title','$description','$image_name','$image_id','$date')";
mysqli_query($conn, $sqli) or die ("No query");
header('location:index.php');
//$profile_pic_name = @$_FILES["profilepic"]["name"];
//$img_id_before_md5 = "$rand_dir_name/$profile_pic_name";
//$img_id = md5($img_id_before_md5);
//$profile_pic_query = mysql_query("INSERT INTO photos VALUES ('','test','$user','$date','$description','http://localhost/tutorials/findFriends/userdata/user_photos/$rand_dir_name/$profile_pic_name','no','$img_id')");
//header("Location: upload_photo.php");
}
} else {
header('location:error.php');
}
?>
答案 0 :(得分:0)
如果我正确读取此权限,您将$ image_id作为md5哈希插入数据库,那么您的
<img src='$image_id'>
未指向实际图像。