Search code examples
phpdirectorydir

how to show thumb path photos in php


how to show thumb folder images in php

i have a folder name albums and in album each folder have photos & thumb folder photos

example

  • uae/thumb/1.jpg
  • uae/1.jpg
  • uk/thumb/1.jpg
  • uk/1.jpg
  • usa/thumb/1.jpg
  • usa/1.jpg

and my code is showing this photos only

  • usa/1.jpg
  • uk/1.jpg
  • uae/1.jpg

and i want to show only thumb photos

  • usa/thumb/1.jpg
  • uk/thumb/1.jpg
  • uae/thumb/1.jpg

how can i do this please help me to fix this issue thanks in advance

here is code

<?php

    //path to directory to scan. i have included a wildcard for a subdirectory
    $directory = "albums/*/";

    //get all image files with a .jpg extension.
    $images = glob("" . $directory . "*.jpg");

array_multisort(array_map('filemtime', $images), SORT_DESC, $images);

?>

<?php $num_of_files = 0; $i=0; foreach ($images as $image):?>
<div class="item"><a href="<?php echo basename(pathinfo($image, PATHINFO_DIRNAME)); ?>.html" target="_blank">
<img class="lazyOwl" src="<?php echo $image ?>" />
<p><?php echo basename(pathinfo($image, PATHINFO_DIRNAME)); ?></p>
             </div>
<?php if (++$num_of_files == 3)break; ?>
  <?php if(++$i%3==0): ?>

<?php endif ?>

<?php endforeach ?>

Solution

  • Adjust your directory path:

    $directory = "albums/*/thumb/";
    
    //get all image files with a .jpg extension.
    $images = glob($directory . "*.jpg");
    

    ... or try using SPL tools:

    $directory = new RecursiveDirectoryIterator('albums/*/'); // or ('albums/*')
    $iterator = new RecursiveIteratorIterator($directory);
    $images = new RegexIterator($iterator, '/thumb\/\(.jpe?g|.png)$/i', RecursiveRegexIterator::GET_MATCH);
    $images = iterator_to_array($images);
    ...
    // iterating over $images