【问题标题】:Failing to display image from server directory file无法从服务器目录文件显示图像
【发布时间】:2020-02-19 18:24:39
【问题描述】:

我无法显示“上传”文件夹的内容。

在网站上我上传了图片,它进入服务器目录文件/上传文件夹,然后它应该获取并显示图片,但它没有。感谢您阅读本文,很高兴听到您的建议。

    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));


//This is me trying to display the image (all this is in the index.file <main> where my server.php file is included (<?php require_once 'server.php'; ?>)) - 
    <td><?php echo '<img src="'.$target_dir.'" width="100" height="100">'; ?></td>



<main>
<?php require_once 'edit-carteri-server.php'; ?>    

<?php

if (isset($_SESSION['message'])): ?>    

<div class="alert alert-<?=$_SESSION['msg_type']?>">    

    <?php
        echo $_SESSION['message'];      
        unset($_SESSION['message']);
    ?>
</div>
<?php endif ?>

<div class="container">
<?php
    $mysqli = new mysqli('localhost', 'root', '', 'id12522465_admin') or die(mysqli_error($mysqli));        //Savieno ar datubāzi
    $result = $mysqli->query("SELECT * FROM carteri") or die($mysqli->error);                      
 ?>

<div class="row justify-content-center">    
    <table class="table">                   
        <thead>
            <tr>
                <th>Bilde</th>
                <th>Valsts</th>
                <th>Teksts</th>
                <th colspan="2">Action</th>
            </tr>
<?php

    while ($row = $result->fetch_assoc()): ?>       
        <tr>
            <td><?php echo '<img src="'.$target_file.'" width="100" height="100">'; ?></td>
            <td><?php echo $row['valsts']; ?></td>
            <td><?php echo $row['teksts']; ?></td>
            <td>
                <a href="edit-carteri.php?edit=<?php echo $row['id']; ?>"
                    class="btn btn-info">Edit</a>
                <a href="edit-carteri-server.php?delete=<?php echo $row['id']; ?>"
                    class="btn btn-danger">Delete</a>
            </td>
        </tr>
<?php endwhile; ?>
        </thead>
    </table>
</div>

【问题讨论】:

    标签: php mysql image server


    【解决方案1】:

    我想你想要:

    <td><?php echo '<img src="'.$target_file.'" width="100" height="100">'; ?></td>
    

    不是:

    <td><?php echo '<img src="'.$target_dir.'" width="100" height="100">'; ?></td>
    

    【讨论】:

    • 嗨,它告诉我 $target_dir 是一个未定义的变量,与 $target_file 相同,我将发布包含包含部分的整个代码。查看我的原始帖子以查看它。
    • 当您在第一行代码中明确定义 $target_dir 时,它被报告为未定义,这很奇怪。您的问题中显示的所有代码是否都在同一个文件中?
    • 没有服务器代码在“edit-carteri-server.php”中,其余的都在索引文件中。
    • 让我们看看您的require_once 是否按您期望的方式工作。在此行之后:&lt;?php require_once 'edit-carteri-server.php'; ?&gt; 放入 echo $target_dir 看看你会得到什么。
    • 那么您的require_once 没有按预期工作。请编辑您的问题以包含edit-carteri-server.php 的全部内容。您还应该清理表格:所有内容都在&lt;thead&gt; 中。没有&lt;tbody&gt;。您的 &lt;main&gt; 标签未关闭。你可能想停下来做一个小测试页来消除除require...之外的所有内容,直到你弄清楚为止。
    【解决方案2】:

    从那里的代码 sn-p 看来,您可能只是使用了错误的变量,除非这是添加问题时的错误。

    在图片标签中应该是 $target_file 而不是 $target_dir:

    <td><?php echo '<img src="'.$target_file .'" width="100" height="100">'; ?></td>
    

    【讨论】:

    • 嗨,它告诉我 $target_dir 是一个未定义的变量,与 $target_file 相同,我将发布包含包含部分的整个代码。查看我的原始帖子以查看它。
    • 你的 require_once 工作了吗?您可能想尝试在 require 之后立即打印出 $target_file 以查看它是否设置为任何内容。 edit-carteri-server.php 是否与包含它的文件位于同一目录中?如果没有,您需要输入文件的完整路径。
    【解决方案3】:

    要显示图像,src 必须指向图像的路径。 尝试将$target_dir 替换为$target_file

    【讨论】:

    • 嗨,它告诉我 $target_dir 是一个未定义的变量,与 $target_file 相同,我将发布包含包含部分的整个代码。查看我的原始帖子以查看它。
    • 可能问题出在您的upload 目录上。它是否与您的 php 文件处于同一级别?
    • 很抱歉我不太明白你的问题,你能不能通俗点给我解释一下。
    • 上传文件夹也在admin文件夹中
    猜你喜欢
    • 1970-01-01
    • 2021-04-05
    • 2023-01-15
    • 2019-12-14
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多