dti3914 2018-02-05 05:52
浏览 52
已采纳

从文件夹动态加载图像时,会出现两个带点的图像

I'm trying to load images from a folder on the local server. Image

But there are only the 3 images that appear correctly in the folder.

<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>

EDIT: I was able to fix this by changing the above code to the following...

<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>

Just checking the file extensions.

  • 写回答

1条回答 默认 最新

  • doulin2025 2018-02-05 07:43
    关注

    . and .. define the current dir and the parent dir from your current path. scandir fetches all files and folders in your path, so those are displayed as images as well, since you never check for a valid file extension (defined in your $file_display). To get rid of . and .. you can use

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本