garb images from subfolders

I have a script that grabs images from a folder called “images” and displays them. I would like to modify it to also display the images in all of the subfolders. Can someone help me?

[php]<?php

ini_set( “display_errors”, 0);
//Your folder
$files = glob(“images/.”);

function sortnewestfilesfirst($a, $b) {
return filemtime($b) - filemtime($a);
}
usort($files, “sortnewestfilesfirst”);

$colCnt=0;
echo ‘

’;

for ($i=0;$i<60;$i++)
{
$colCnt++;
if ($colCnt==1)
echo ‘

’;
echo ‘’;

if ($colCnt==6)
{
echo ‘

’;
$colCnt=0;
}
}

echo ‘

’;

$num = $files[$i];

echo ’

'."  ";

echo ‘

’;
?>[/php]

I personally never had a use for this type of coding myself yet… but from what i can gather from a online tutorial on Google… credit: http://www.linuxweblog.com/node/327

File Name: list_pics.php
[php]<?php
// file name: list_pics.php

global $startDir;

/**

  • List Directories function, which transverses sub-folders

  • listing out the image files that exists within it.
    */
    function listDir( $path ) {
    global $startDir;
    $handle = opendir( $path );
    while (false !== ($file = readdir($handle))) {
    if( substr( $file, 0, 1 ) != ‘.’ ) {
    if( is_dir( $path.’/’.$file ) ) {
    listDir( $path.’/’.$file );
    }
    else {
    if( @getimagesize( $path.’/’.$file ) ) {

       /*
       // Uncomment if using with the below "pic.php" script to
       // encode the filename and protect from direct linking. 
       $url = 'http://domain.tld/images/pic.php?pic='
              .urlencode( str_rot13( substr( $path, strlen( $startDir )+1 ).'/'.$file ) );
       */
    
       $url='http://domain.tld/images/'. 
       substr( $path, strlen( $startDir )+1 ).'/'.$file;
    
       // You can customize the output to use img tag instead.
       echo "<a href='".$url."'>".$url."</a><br>";
     }
    

    }
    }
    }
    closedir( $handle );
    } // End listDir function

$startDir = ‘.’;
listDir( $startDir );

?>
[/php]

File Name: pic.php
[php]<?php
// file name: pic.php

$f = trim( $_GET[‘pic’] );
if( ( substr( $f, 0, 1 ) == ‘.’ ) || ( substr( $f, 0, 1 ) == ‘/’ ) ) {
return false;
} else if( !( $imgType = exif_imagetype( str_rot13( $f ) ) ) ) {
return false;
} else {
switch( $imgType ) {
case 1:
header( ‘Content-type: image/gif’ );
break;
case 2:
header( ‘Content-type: image/jpeg’ );
break;
case 3:
header( ‘Content-type: image/png’ );
break;
default:
return false;
break;
}
readfile( str_rot13 ( $f ) );
}
?>
[/php]

Here is an example of how to scan all sub-directories using PHP’s RecursiveDirectoryIterator class

[php]
$path = ‘images/’; // define the path to your images folder
$objs = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objs as $obj) {
if (!$obj->isFile()) {
continue; // skip all directories
}
// check if file is an image type
$extension = pathinfo($obj->getPathname(), PATHINFO_EXTENSION); // get extension
if (in_array($extension, array(‘jpg’, ‘gif’, ‘png’))) {
// this is where you would output your IMG code
echo $obj->getPathname() . “\n”;
}
}
[/php]

You can learn more about the Standard PHP Library (SPL) including the two iterators used here:

http://www.php.net/manual/en/book.spl.php

Sponsor our Newsletter | Privacy Policy | Terms of Service