[PHP] Making a gallery, need help sorting files and displaying time uploaded

Good evening comrades! I am currently setting up a photo gallery and all is running smoothly. However, I’m having some trouble organizing my photographs and folders alphabetically. The script reads from a directory I define and displays the images and folders that are in the directories.

Here’s the script:

<?php
// Globals - Do not tamper with these
global $gallery_root, $gallery_address, $file, $excluded_folders, $pictwidth, $pictheight, $thumbwidth, $thumbheight, $gallery_width;

// ---------- Settings ----------
// Configure these settings, to use the gallery.
 
// Your gallery folder (this is where your pictures and picture folders are located).
$gallery_address = '/images/photos/';


// Add foldernames to exclude. (add more lines like this on more excludes.)
$excluded_folders[] = 'cgi-bin';

// Picture size
$pictwidth = 400;
$pictheight = 400;

// Thumbnail size
$thumbwidth = 200;
$thumbheight = 200;
// ---------- Settings end ----------

$gallery_root = $_SERVER['DOCUMENT_ROOT'].$gallery_address;
$gallery_address = 'http://'.$_SERVER['HTTP_HOST'].$gallery_address;

function showGallery() {
    global $file;
    
    if (!validateFile())
    {
        echo 'Invalid folder or file';
        return;
    }
    createNavigation();

    $path = pathinfo($file);
    if ($path['extension'] == '')
    {
    //Display Dir(s) (if any)
    showDirs();

    //Display Thumb(s) (if any)
    showThumbs();

    } else {
    showSlide($file);

    }
}

function setCurrentdir()
{
    global $currentdir, $file;
    $path = pathinfo($file);
    if ($path['extension'] != '')
        $currentdir = $path['dirname'].'/';
    else
        $currentdir = $file;
}

function showDirs() {

    global $gallery_root, $currentdir, $file, $excluded_folders;
    $runonce = false;
    if ($dir_content = opendir($gallery_root.$currentdir)) {
        
        while ( false !== ($dir = readdir($dir_content)) ) {
            if (is_dir($gallery_root.$currentdir.$dir) && !in_array($dir, $excluded_folders) && $dir!='.' && $dir!='..' ) {
                if ( !$runonce ){
                    echo '<div class="folder">Folders:<br/><table class="foldersbox">';
                    $runonce = true;
                }
                echo '<tr><td><a href="'.$_SERVER['PHP_SELF'].'?file='.$currentdir.$dir.'/"><img src=http://www.saurdo.com/images/folder.gif> '.$dir.'</a></td></tr>';
            }
        }
       }
    if ( $runonce )
        echo '</table></div><br/>';
}

function showSlide($slidefile) {
    
    global $gallery_root, $gallery_address, $currentdir, $file;
    if ($dir_content = opendir($gallery_root.$currentdir)) {
        while ( false !== ($img = readdir($dir_content)) ) {
            if ( is_file($gallery_root.$currentdir.$img) && eregi(".*(\.jpg|\.gif|\.png|\.jpeg)", $img))
                    $imgfiles[] = $img;
        }
    }

    $prev = '';
    $slide = '';
    $next = '';

    $arraysize = count($imgfiles);
    
    for ($i=0; $i < $arraysize; $i++) 
    {
    
        if($currentdir.$imgfiles[$i] == $slidefile)    
        {
            $slide = $imgfiles[$i];
            // Set prev
            if($i==0)
                $prev = $imgfiles[$arraysize-1];
            else
                $prev = $imgfiles[$i-1];
            // Set Next
            if($i+1 == $arraysize)
                $next = $imgfiles[0];
            else
                $next = $imgfiles[$i+1];                
        }
    }
    
    // Get picture info
    $img = $gallery_root.$currentdir.$slide;
    $path = pathinfo($img);
    switch(strtolower($path["extension"])){
        case "jpeg":
        case "jpg":
            $img=imagecreatefromjpeg($img);
            break;
        case "gif":
            $img=imagecreatefromgif($img);
            break;
        case "png":
            $img=imagecreatefrompng($img);
            break;
        default:
            break;            
    }
    $xsize = (imagesx($img));
    $ysize = (imagesy($img));
    imagedestroy($img);
    
    echo '<div id="nav">
        <a href="'.$_SERVER['PHP_SELF'].'?file='.$currentdir.$prev.'" class="alignleft">&laquo; Prev</a>
        <a href="'.$_SERVER['PHP_SELF'].'?file='.$currentdir.$next.'" class="alignright">Next &raquo;</a>
        </div>
        <br/>
        <div class="image"><a href="'.$gallery_address.$currentdir.$slide.'" target="_new"><img src="./wp-content/plugins/gallery/img.php?file='.$currentdir.$slide.'&thumb=0"></a>
        </div>
        <br/>
        <div class="imgdata">
        Image data<br/>
        Actual size: '.$xsize.' x '.$ysize.'<br/>
        </div>';
}

function showThumbs() {
    
    global $gallery_root, $currentdir, $file, $thumbwidth, $thumbheight;
    
    if ($dir_content = opendir($gallery_root.$currentdir)) {
        while ( false !== ($file = readdir($dir_content)) ) {
            if ( is_file($gallery_root.$currentdir.$file) )
                if(eregi(".*(\.jpg|\.gif|\.png|\.jpeg)", $file))
                    $imgfiles[] = $file;
        }
    }
    echo '<div class="image">Images<p><p>&nbsp;</p>';
    if(isset($imgfiles))
    {
        foreach ($imgfiles as $img)
        {
            //echo '<div style="position: relative; width: '.($thumbwidth+16).'px; height: '.($thumbheight+16).'px; border: 1px solid #a9a9a9;">';
            echo '<a href="'.$_SERVER['PHP_SELF'].'?file='.$currentdir.$img.'" class="linkopacity"><img src="./wp-content/plugins/gallery/img.php?file='.$currentdir.$img.'&thumb=1"></a>';
            //echo '</div>';
        }
    }
    echo '</div>';
}

// Validates file variable
function validateFile() {

    global $excluded_folders, $file;
    $file = $_GET['file'];
    
    // validate dir
    if ( strstr($file, '..') || strstr($file, '%2e%2e') )
        return false;
        
    foreach ($excluded_folders as $folder)
    {
        if ( strstr($file, $folder) )
        return false;
    }
    setCurrentdir();
    return true;
}

function createNavigation()
{
    global $currentdir, $file;

    if ($currentdir == './')
    $currentdir = '';
    $nav = split('/', $currentdir);
    array_pop($nav);
    $path = pathinfo($file);
    
    echo '<div>Now Viewing: &raquo; <a href="'.$_SERVER['PHP_SELF'].'">Gallery</a> ';
    foreach ($nav as $n)
    {
        $current .= $n.'/';
        echo '&raquo; <a href="'.$_SERVER['PHP_SELF'].'?file='.$current.'">'.$n.'</a> ';
    }
    if ($path['extension']!='')
        echo '&raquo; <a href="'.$_SERVER['PHP_SELF'].'?file='.$current.$path['basename'].'">'.$path['basename'].'</a>';
    echo '</div>';
}
?>

I’ve tried sort($dir); , sort($dir_content); , sort($currentdir.$dir); , and so on and so forth. None of them have worked. I’m placing it right after the loops as I’ve learned through example from another script. Can someone please explain to me the correct way to go about this and what I’m doing wrong?

Another not so related thing is displaying the time/date that a file was uploaded. I’ve seen another script that can do this but instead of enlightening me it confused me. If someone could show me how to do this as well then you’d save me a lot of time and trouble.

My Gallery is located here: http://www.saurdo.com/gallery.php

Thanks for your time!