【问题标题】:how to display an image from folder with php?如何使用php显示文件夹中的图像?
【发布时间】:2023-03-08 16:31:01
【问题描述】:

你们能帮帮我吗?我正在尝试从文件夹中检索图像,但图像不会出现。

这是我的代码:

我正在使用表格“文章”:

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) -> 处理文章的标题、图片和内容

$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 是什么?服务器对该请求的响应是什么?那真的是文件所在的位置吗?
  • 在浏览器中打开并检查 url 是否正确。此外,如果您在 php 中动态提供图像,则需要检查 http 标头中图像的 mime 类型
  • 答案是将 sql 查询中的 'assets/news_image/$fileName' 更改为 '/assets/news_image/$fileName',这样您就可以使用绝对 url 而不是破坏图像的相对 url

标签: php image web upload directory


【解决方案1】:

似乎路径问题。

我可以看到您在表格中插入图片路径,例如 'assets/news_image/$fileName',但您正在上传图片 '../assets/news_image/'

将文件路径更新为:-

对于绝对路径(推荐):

echo "<img src="."/". $row['image'] . " />";

相对路径:

echo "<img src="."../". $row['image'] . " />";

希望这会帮助你(y)。

【讨论】:

  • 只编辑和添加绝对网址,不要使用相对网址。他们总是制造问题
  • @georoot 确定(y)。
  • @georoot 我已将 sql 查询更改为 VALUES('$tit','$fileName','../assets/news_image/$fileName',并使用 echo ""; 图像出现了......谢谢大家:D
  • @Arnold 很高兴听到这个答案很有帮助。仅供参考,看看 MVC 架构,因为您的应用程序结构将来很难维护:)
  • @georoot 模型视图控制器,就像在 CodeIgniter 中一样?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-11
  • 2018-02-01
  • 2013-11-18
  • 2015-09-23
相关资源
最近更新 更多