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 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页