【问题标题】:remove certain items when printing a directory list打印目录列表时删除某些项目
【发布时间】:2012-08-18 08:33:49
【问题描述】:

我让用户使用打开和读取目​​录的下拉菜单从目录中选择特定文件。

目录列表中有一些不应该存在的项目。像

这样的项目
.
..
.com.apple.timemachine.supported
.DS_Store

我将如何删除这些?它们看起来像目录命令或信息或其他东西。用户不应该能够选择这些,即使我认为他们永远不会。

这是我用来读取目录并将项目打印到下拉列表中的代码。

<div id='fileOpen'>    
<?
    $pathToImages = 'images/';
    $pathToVolume = '/Volumes/storage/spots_in/';

    if ($handle = opendir($pathToVolume)) {
?>
    <span class='locate'>Locate master file:</span>
    <select id='file' name='file'> 
<?
    while (false !== ($entry = readdir($handle))) {
        echo "<option>";
        echo "$entry\n";
        echo "</option>";
    }
    closedir($handle);
 }
</div>

【问题讨论】:

    标签: php html directory


    【解决方案1】:

    我用这个

    if (!preg_match("/^\./","$entry\n"))
    

    【讨论】:

    • 不用担心。如果你只想要文件,你也可以做一个 is_file 检查,但 preg 对我来说很好。
    【解决方案2】:
    while (false !== ($entry = readdir($handle))) {
        if (substr($entry, 0, 1) == '.') {
            continue;
        }
    
        /* Other stuff
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-24
      • 2013-05-28
      • 2020-03-25
      • 1970-01-01
      • 2021-12-08
      • 2014-06-25
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多