dongqin1861 2013-05-19 10:46
浏览 33
已采纳

将file_exists用于一系列文件名的简明方法

I am using file_exists to determine whether certain image files are in the specified directory, and if they are, displaying them within an image rotator. It works fine but the code seems very long winded for what it achieves. Is there a better way?

At the moment I have:

<?php
        if (file_exists($reg_photo_1_f)) {
        echo "<li>";
        echo "<a href=". $reg_photo_1_f .">"."</a>";
        echo "</li>";
        }
        if (file_exists($reg_photo_2_f)) {
        echo "<li>";
        echo "<a href=". $reg_photo_2_f .">"."</a>";
        echo "</li>";
        }
        if (file_exists($reg_photo_3_f)) {
        echo "<li>";
        echo "<a href=". $reg_photo_3_f .">"."</a>";
        echo "</li>";
        }
        if (file_exists($reg_photo_4_f)) {
        echo "<li>";
        echo "<a href=". $reg_photo_4_f .">"."</a>";
        echo "</li>";
        }
        if (file_exists($reg_photo_5_f)) {
        echo "<li>";
        echo "<a href=". $reg_photo_5_f .">"."</a>";
        echo "</li>";
        }
        ?>

Where each $reg_photo_x_f is the image path. I'm hoping there is a way that I can adapt this code so that it can loop until all images are found to exist. The image names themselves are hashes generated from another page, so are obviously not consecutive.

The page creating the hashes combines a value relevant to the page in the form of a php variable with a pre-defined 'salt' so that the image name is matched almost in the same way as matching a hashed password in a database etc. For example, for $reg_photo_1_f I have this code on the same page:

$reg_photo_1 = sha1("$bh".PHOTO_REG_1);
$reg_photo_1_f = $reg_photo_root.$reg_photo_1.".jpg";

...where $bh is the variable relevant to the page the user is on and PHOTO_REG_1 is the 'salt'.

Sure there is a much better way to name the images too but this was the simplest way I could think of for it to work with dynamic pages.

  • 写回答

1条回答 默认 最新

  • dpvm7231 2013-05-19 10:59
    关注
    <?php
    
    $files = glob('*.jpg');
    foreach($files as $file) {
            echo "<li>";
            echo "<a href=". $file.">"."</a>";
            echo "</li>";
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解