tcd
tcd

Reputation: 1605

automatic image gallery insert

I have this code set up for inserting any image that is uploaded to my "images" folder, right into a gallery I have.... My problem is that it kind of inserts them randomly.. I would like to set it up to insert the most recently uploaded picture to the end of the gallery "row", any suggestions? Thanks

<?php

    $image_dir = 'uploads/images/';
    $per_column = 10;       

    if ($handle = opendir($image_dir)) {
        while (false !== ($file = readdir($handle))) 
        {
            if ($file != '.' && $file != '..') 
            {
                if(strstr($file,'.png'))
                {
                    $files[] = $file;
                }
                if(strstr($file,'.jpg'))
                {
                    $files[] = $file;
                }
                if(strstr($file,'.gif'))
                {
                    $files[] = $file;
                }
                if(strstr($file,'.jpeg'))
                {
                    $files[] = $file;
                }
            }
        }
        closedir($handle);
    }

    if(count($files))
    {
        foreach($files as $file)
        {
            $count++;
            echo '<li><img src="',$image_dir,$file,'" width="20" height="20" title="',$file,'"/></li>';
            if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
        }
    }
    else
    {
        echo 'no pictures yet...';
    }

?>

Upvotes: 1

Views: 647

Answers (2)

Book Of Zeus
Book Of Zeus

Reputation: 49877

<?php

    $image_dir = 'uploads/images/';
    $per_column = 10;       

    $validExt = array(
        'png' => 'image/png',
        'jpeg' => 'image/jpeg',
        'jpg' => 'image/jpg',
        'gif' => 'image/gif',
    );

    if ($handle = opendir($image_dir)) {
        while (false !== ($file = readdir($handle))) 
        {
            $ext = strtolower(substr($file, -3));
            if (isset($validExt[$ext])) 
            {
                $stats = stat($image_dir.$file);
                $files[$stats['mtime']] = $file;
            }
        }
        closedir($handle);
    }

    $count = 0;
    krsort($files);

    $cnt = count($files);

    if($cnt)
    {
        foreach($files as $file)
        {
            $count++;
           echo '<li><img src="' . $image_dir . $file . '" width="20" height="20" title="' . substr($file, 0, -4) . '"/></li>'.chr(10);
            if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
        }
    }
    else
    {
        echo 'no pictures yet...';
    }

Upvotes: 4

Bojangles
Bojangles

Reputation: 101473

I'm not sure what your file format is, but try setting the filename to the current time using time() when uploading.

When you list your images, try using glob() instead of readdir.

Your code might then go as follows (untested):

$image_dir = 'uploads/images/';
$per_column = 10;       

$files = glob($image_dir . "*.jpg|png|gif|jpeg");

if(count($files))
{
    foreach($files as $file)
    {
        $count++;
        echo '<li><img src="',$image_dir,$file,'" width="20" height="20" title="',$file,'"/></li>';
        if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
    }
}
else
{
    echo 'no pictures yet...';
}

Coupling this with giving the uploaded file a filename of the current time, this should order your images by most recent.

The most important line here is $files = glob($image_dir . "*.jpg|png|gif|jpeg");. This will scan the directory for any file (*) ending in .jpg, .png, .gif or .jpeg. You don't need to worry about . and ..; the expression filters that out.

Upvotes: 0

Related Questions