PHP script to display all the images in the directory?

RavinDJ

Supreme [H]ardness
Joined
Apr 9, 2002
Messages
4,578
I need a PHP script to display all the images in the directory... anyone can point me in the right direction?

Or post code :p
 
This is how I do it right now:
Code:
// Set this to the directory that you want to list the images in
  define('IMAGE_DIRECTORY',PATH . 'images/');
// Set this to the URL prefix required (if any). 
  define('URL_PREFIX', BASE_PATH . 'images/');
// Set this 'true' to look for images in subdirectories
  define('RECURSIVE',true);


function echo_images_in_dir($dirname,$recurse,$prefix = '') {
    if( is_dir( $dirname ) ) {
        $dir = opendir($dirname);
        while( FALSE !== ( $entry = readdir($dir ) ) ) {
            if ($entry == '.' or $entry == '..') continue;
            if (is_dir($dirname.'/'.$entry)) {
                if ($recurse)
                   echo_images_in_dir($dirname.'/'.$entry,$recurse,$prefix.basename($entry).'/');
                } elseif (is_image($dirname.'/'.$entry))
                    echo( '<tr valign="top"><td width="50%" align="left"><div>' . URL_PREFIX . $prefix.basename($entry) .'</div></td><td align="left"><img src = "' . URL_PREFIX . $prefix.basename($entry) . '"/></td></tr>' );
                }
        }
}

function is_image($filename) {
    $ext = strtolower(substr($filename,-4));
    return is_file($filename) and is_readable($filename) and ($ext == '.gif' or $ext == '.jpg' or $ext == '.png');
}

// then use this call:
echo_images_in_dir(IMAGE_DIRECTORY,RECURSIVE);

HTH
 
Thanks for all the input!!!

Yeah, I looked around @ HotScripts... and thanks for the code, too!!!!
 
Back
Top