显示子文件夹的 1 张图像


Display 1 image of subfolder

我想显示多个子文件夹的一个(第一个)图像,我有这个来显示所有子文件夹中的所有文件其他(更好的)解决方案也非常受欢迎

$path = realpath('img/gallery');
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path),RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
  $example = '/somestring/';
  $trimmed =  str_replace("/somestring/", "", $name);
  echo "<img src=".$trimmed." /></br>";
  //...
}

画廊现在看起来像这样

        foreach ($images as $img) { ?>
<ul class="list_4">
    <li>
        <div class="block_pic">
        <?php echo "<img src='$img' alt='whatever' /></br>"; ?>
        </div>
    </li>
</ul>
     <?php      }
            }
        }
    ?>

当前网页

<ul class="list_4">
    <li>
        <div class="block_pic">
        <?php
        echo "<a href=".$trimmed."><img src=".$trimmed." alt=' '></a>";
        //echo "<img src=".$trimmed." />";
        ?>
        </div>
    </li>
</ul>

如果您确定每个子目录只包含 JPEG 图像,那么您RecursiveDirectoryIterator方法是一个良好的开端。 我会坚持这一点,但要跟踪您已经访问过哪些目录并显示一张图像,请维护一个存储文件名目录部分的数组,您可以轻松地从pathinfo($name, PATHINFO_DIRNAME)或更简单地从dirname()获取。

下面的代码经过测试,并且可以处理我的图像文件目录树之一:

// Array to hold visited directories
// Each iteration will check this array for the
// current directory. If already present, it will be skipped
// and if not, action taken.
$visited_dirs = array();
$i = new RecursiveDirectoryIterator(realpath('img/gallery'), RecursiveDirectoryIterator::SKIP_DOTS);
// Set the SKIP_DOTS flag to avoid '.' and '..'
// This method is available in PHP 5.3+
// $i->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
// Define the recursive iterator
$recursive = new RecursiveIteratorIterator($i);
// Loop over it
foreach ($recursive as $path => $object) {
  // Get the directory
  $dir = pathinfo($path, PATHINFO_DIRNAME);
  // Only do something if this directory hasn't already been visited
  if (!in_array($dir, $visited_dirs)) {
    // Add to the list of visited dirs so it isn't used again
    $visited_dirs[] = $dir;
    // Do whatever you need to with the file for output
    // String manipulations, etc...
    $trimmed =  str_replace("/somestring/", "", $path);
    echo "<img src=".$trimmed." /></br>";
  }
}

关于排序:

请注意,以上内容可能无法获得所需的排序。如果您需要让它们在子目录中按字母顺序排序,您可以存储一个包含所有看到的文件的数组,然后sort()它,然后使用与上面类似的数组逻辑循环:

$visited_dirs = array();
// Array of all files
$all_files = array();
// $recurisve is the RecursiveIteratorIterator declared as
// in the first example
foreach ($recursive as $path => $object) {
  if ($object->isFile()) {
     // keep an array of all files
     $all_files[] = $path;
  } 
}
// Sort it
sort($all_files);
// Then loop and output just as in the last example
// but iterate the array
foreach ($all_files as $path) {
  $dir = pathinfo($path, PATHINFO_DIRNAME);
  if (!in_array($dir, $visited_dirs)) {
    $visited_dirs[] = $dir;
    $trimmed =  str_replace("/somestring/", "", $path);
    echo "<img src=".$trimmed." /></br>";
  }
}

如果您愿意,可以将所有$trimmed值存储到另一个数组中,而不是直接在循环中echo它们。这将使以后更容易使用它们。我可能会这样做。

// Instead of directly echo...
$images[] = $trimmed;
// Later loop them to make use of them in a *different loop*
foreach ($images as $img) {
   echo "<img src='$img' alt='whatever' /></br>";
}