你们能帮我吗?我试图从文件夹中检索图像,但图像不会显示出来。如何使用php显示来自文件夹的图像?
这里是我的代码:
我使用表 “文章”:
id int(100) auto increment,
title varchar(150),
imageName varchar(250) -> to store the file name,
image varchar(250) -> to store the image location address,
content text
这里是我的文件夹结构:
(artikel_created.php) - > proccess文章标题,图片和内容
$tit = mysqli_real_escape_string($con, $_POST['title']);
$cont = mysqli_real_escape_string($con, $_POST['content']);
$fileName = $_FILES['image']['name']; //get the file name
$fileSize = $_FILES['image']['size']; //get the size
$fileError = $_FILES['image']['error']; //get the error when upload
$tmp = $_FILES['image']['tmp_name'];
?>
<?php include 'admin_header.php'; ?>
<body>
<section class="body">
<div id="log"></div>
<div id="artikel_created_panel">
<?php
if ($fileSize > 0 || $fileError == 0) {
$move = move_uploaded_file($tmp, '../assets/news_image/' . $fileName);
if ($move) {
$result = mysqli_query($con, "INSERT into articles(title, imageName, image, content) VALUES('$tit','$fileName','assets/news_image/$fileName', '$cont')");
if (!$result) {
trigger_error("Query Failed! SQL: $result - Error: " . mysqli_error($con), E_USER_ERROR);
} else {
echo "<br/>";
echo "Artikel added succesfully";
echo "<br/>";
}
}
}
?>
<a href=../admin/admin_panel.php>Back to admin panel</a>
</div>
</section>
和这里的(artikel.php)显示文章
if (isset($_GET['id'])) {
$id = $_GET['id'];
$qry = mysqli_query($con, "SELECT * FROM articles WHERE id=$id");
if (!$qry) {
trigger_error("Query Failed! SQL: $qry - Error: " . mysqli_error($con), E_USER_ERROR);
}
/*Fetching data from the field "title"*/
while ($row = mysqli_fetch_array($qry)) {
echo "<h2>" . $row['title'] . "</h2>";
echo "<img src=" . $row['image'] . " />";
echo "<p>" . $row['content'] . "</p>";
}
}
?>
以下是图像:
你们能告诉我什么是错的代码吗?
在'img'元素中使用的结果URL是什么?服务器对该请求的回应是什么?这真的是文件所在的位置吗? – David
在浏览器中打开并检查网址是否正确。另外,如果你在php中动态地提供图片,你需要检查HTTP头中的MIME类型的图像 – georoot
答案是在sql查询中将''assets/news_image/$ fileName''改为''/ assets/news_image/$ fileName '''''''''''''''''这样你使用绝对url而不是相对的图片 – georoot