简体   繁体   中英

Display 1 image of subfolder

I would like to display one(first) image of multiple subfolders, I've got this to display all files from all subfolders other (better) solutions are also very welcome

$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>";
  //...
}

The gallery looks now like this

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

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

If you are certain that each subdirectory only contains JPEG images, your RecursiveDirectoryIterator approach is a good start. I'd stick with that, but to keep track of which directories you have already visited and displayed one image from, maintain an array which stores the directory portion of the filename, which you can easily get from pathinfo($name, PATHINFO_DIRNAME) or more simply from dirname() .

The code below is tested and working on one of my directory trees of image files:

// 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>";
  }
}

On sorting:

Note that the above may not get you the sort you want. If you need to get them to sort alphabetically within subdirectories, you may store an array of all the files seen then sort() it, before looping over with similar array logic as above:

$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>";
  }
}

If you preferred, you could store all of the $trimmed values into another array rather than echo them directly in the loop. That would make it easier to use them all later. I would probably do it that way.

// 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>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM