【问题标题】:While loading images dynamically from folder, two images appear with dots从文件夹动态加载图像时,两个图像带有点
【发布时间】:2018-02-05 05:52:52
【问题描述】:

我正在尝试从本地服务器上的文件夹加载图像。

但文件夹中只有 3 张图片正确显示。

<table class="table">
    <?php 
    $dir  = '../images/slideshow';
    $file_display = array('jpg', 'jpeg', 'png', 'gif');

    if (file_exists($dir) == false) {
        echo 'Directory \''. $dir. '\' not found!';
    } else {
        $dir_contents = scandir($dir);

        foreach ($dir_contents as $file) {
            $ex = explode('.', $file);
            $file_type = strtolower(end($ex));

            ?>
            <tr>
            <td><img alt="<?php echo $file?>" width="64px" height="64px" src="<?php echo $dir."/".$file?>">
            <td class="td" style="text-align: center;"><a href="<?php echo $_SERVER['PHP_SELF']."?action=delete&id=".$row["id"]; ?>" class="btn btn-default btn-danger" >Delete</a><?php
        }
    }
    ?>
</table>

编辑: 我可以通过将上面的代码更改为以下代码来解决此问题...

<table class="table">
    <?php 
    $dir  = '../images/slideshow';
    $file_display = array('jpg', 'jpeg', 'png', 'gif');

    if (file_exists($dir) == false) {
        echo 'Directory \''. $dir. '\' not found!';
    } else {
        $dir_contents = scandir($dir);
        $file_display = array(
                'jpg',
                'jpeg',
                'png',
                'gif'
        );

        foreach ($dir_contents as $file) {
            $ex = explode('.', $file);
            $file_type = strtolower(end($ex));
            if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) {
                ?>
                <tr>
                <td><img alt="<?php echo $file?>" width="64px" height="64px" src="<?php echo $dir."/".$file?>">
                <td class="td" style="text-align: center;"><a href="<?php echo $_SERVER['PHP_SELF']."?action=delete&id=".$row["id"]; ?>" class="btn btn-default btn-danger" >Delete</a><?php
            }
        }
    }
    ?>
</table>

只检查文件扩展名。

【问题讨论】:

  • 检查您的图像名称和这两个图像中使用的文件夹的路径....
  • 使用开发者工具检查图片路径。右键单击-> 检查元素
  • 同时发布 PHP 代码
  • 你的图片文件名中有空格吗?如果是这样,请删除空格
  • 你的代码在哪里

标签: php html image


【解决方案1】:

... 定义当前路径中的当前目录和父目录。 scandir 获取路径中的所有文件和文件夹,因此它们也显示为图像,因为您从不检查有效的文件扩展名(在 $file_display 中定义)。要摆脱 ... 你可以使用

$dir_contents = array_diff(scandir($directory), array('.', '..'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-28
    • 2012-05-01
    • 2011-07-07
    • 1970-01-01
    • 2012-07-28
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多