Archive for January, 2010

Sorted Array of Images in a Folder (PHP)

January 29th, 2010

<?php
function dirImages($dir)
{
$d = dir($dir); //Open Directory
while (false!== ($file = $d->read())) //Reads Directory
{
$extension = substr($file, strrpos($file, '.')); // Gets the File Extension
if($extension == ".jpg" || $extension == ".jpeg" || $extension == ".gif" || $extension == ".png") // Extensions Allowed
$images[$file] = $file; // Store in Array
}

$d->close(); // Close Directory
asort($images); // Sorts the Array

return $images;    //Author: ActiveMill.com
}

$array = dirImages('/home/your absolute directory/');

foreach ($array as $key => $image) // Display Images
{
echo $image;
echo '</br>';
}
?>