Including sub folders in a directory

Hello,

I am trying to list only .jpg in all sub folders of the directory “MOVIES”. I can get this to work only in the MOVIES directory and not all sub directories. The following is the code I am using.

<?php $folder = "../MOVIES/*/"; // The folder containing the images. $list = array(); // This will hold data for the images found. $valid = array("jpg", "jpeg"); // Images filetypes. // Open the folder and read the files. $dir = opendir($folder); while (($file = readdir($dir)) !== false) { // Make sure the file is an actual file. if (is_file($folder . $file)) { // Check for valid file extensions. if (hasValidExtension($file)) { // Get the image info. // $info->name = $file; // Add the info to the list array. $list[] = $file; } } } // Close the directory. closedir($dir); // Check to make sure a file has a valid extension. function hasValidExtension($file) { global $valid; $dot = strrpos($file, "."); if ($dot === false) { return false; } $ext = substr($file, $dot+1, 4); foreach ($valid as $value) { if ($ext == $value) { return true; } } return false; } ?> <?php foreach($list as $image){ //print the image to browser with anchor tag (Use if you want really :) ) echo '
'; } ?>

The method I used to display things like this is with scandir, give it a shot. I have probably missed some things out.

[php]$folder = “…/MOVIES/”;
$folder_name = scandir($folder, 0);
foreach ($folder_name as $movie_name) { //Search movie
if ($movie_name === ‘.’ or $movie_name === ‘…’) continue;
$subfolder = “…/MOVIES/’.$movie_name.’”;
$image_file = scandir($subfolder , 0);
foreach ($image_file as $img) { //Search for image file
if (hasValidExtension($img )) {
$list[] = $img;
}
}
}[/php]

Ever heard of DirectoryIterator or it’s recursive counterpart?
Makes doing things like this a whole lot easier.

[php]
// this function takes two args: a string and an array.
// $path = string. The path to a folder.
// $type = array. An array of file types to look for.
function list_files($path, $type) {
$directory = new \RecursiveDirectoryIterator($path);
$iterator = new \RecursiveIteratorIterator($directory);
$files = array();
foreach ($iterator as $info) {
if (in_array($info->getExtension(), $type)) {
$files[] = $info->getPathname();
}
}
return $files;
}

// find me some??
$path = ‘imgs’; // start in this folder/directory
$type = array(‘png’, ‘jpg’); // find me these types of file…
$image_array = list_files($path, $type); // let’s do this…

// display the results
print ‘

’;
print_r($image_array);
print ‘
’;
/*
output:
Array
(
[0] => imgs\cat.png
[1] => imgs\dog.png
[2] => imgs\mouse.jpg
[3] => imgs\more_images\cat2.png
[4] => imgs\more_images\dog2.png
[5] => imgs\more_images\mouse2.jpg
)

[/php]

Easy peasy and much cleaner!
Hope that helps,
Red :wink:

If got it. we are trying to make things to complicated.

[php]<?

//Path to folder which contains images
$dirname = “…/MOVIES/*/”;

//Use glob function to get the files
$images = glob($dirname."*.jpg");

//Display image using foreach loop
foreach($images as $image){

//print the image to browser with anchor tag (Use if you want really :slight_smile: )
echo ‘
’;
}
?>[/php]

Sweet, I’m happy you got it working, as they say, there is more than one way to skin a cat! :slight_smile:

One thing i’d point out though
[php]<? // << Don’t do this! [/php]

If you were to upload your code to a server with short tags off then you will have to go through every page and change all occurrences. - Prevention is better than a cure and for the sake of three chars, is it worth the hassle?

Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service