显示子文件夹的1张图片

时间:2015-01-11 21:31:08

标签: php

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

$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      }
            }
        }
    ?>

当前的HTML

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

1 个答案:

答案 0 :(得分:0)

如果您确定每个子目录只包含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>";
}