Sorting a Image Directory
Posted: Wed Jul 12, 2006 9:42 am
Hi, I am having a problem trying to sort an image directory, i need to be able to sort it by filename first of all but it would help if any one could help sorting by filesize, file modifyed etc.
Here is my code so far
I've had a quick search here and google but have a deadline of a week to get this project finished while learning php as well.
hope you can help
Here is my code so far
Code: Select all
$baseroot = ($_SERVER['DOCUMENT_ROOT']);
$imgdir = ''.$baseroot.'/test2/cms/uploaded_images/'; // the directory, where your images are stored
$allowed_types = array('png','jpg','jpeg','gif','bmp'); // list of filetypes you want to show
$dimg = opendir($imgdir);
while($imgfile = readdir($dimg))
{
if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))
{
$a_img[] = $imgfile;
sort($a_img);
reset ($a_img);
}
}
$totimg = count($a_img); // total image number
for($x=0; $x < $totimg; $x++)
{
$size = getimagesize($imgdir.'/'.$a_img[$x]);
// do whatever
$halfwidth = ceil($size[0]/2);
$halfheight = ceil($size[1]/2);
$cleanFile = pathinfo($a_img[$x]);
$exten = $cleanFile['extension'];
if ($exten=="jpg")
echo "<img src='icons/jpg.gif' alt='JPG'/>";
elseif ($exten=="gif")
echo "<img src='icons/gif.gif' alt='GIF'/>";
elseif ($exten=="jpeg")
echo "<img src='icons/jpg.gif' alt='JPEG'/>";
elseif ($exten=="png")
echo "<img src='icons/png.gif' alt='PNG'/>";
elseif ($exten=="bmp")
echo "<img src='icons/bmp.gif' alt='BMP'/>";
else
echo "";
$filename = ''.$baseroot.'/test2/cms/uploaded_images/'.$a_img[$x].'';
echo $a_img[$x].' ' . filesize($filename) . ' bytes ';
echo "Last change: ".date("d/m/Y H:i",filectime($filename));
echo ' '.$size[0].' x '.$size[1].'';
echo ' <a href=img_delete.php?id='.$a_img[$x].'&accept=NO>Delete</a><br />';
}hope you can help