$dir = 'siteImages'; // open specified directory $dirHandle = opendir($dir); $count = -1; $returnstr = ""; $images = array(); $imagesL = array(); while ($file = readdir($dirHandle)) { // if not a subdirectory and if filename contains the string '.jpg' if(!is_dir($file) && strpos($file, '.jpg')>0) { // update count and string of files to be returned $count++; $returnstr .= '&f'.$count.'='.$file; if (!preg_match("/_large/i", $file, $match)) { $images[] = $file; $imagesL[] = str_replace(".jpg", "_large.jpg", $file); } } } $returnstr .= '&'; //echo $returnstr; closedir($dirHandle); sort($images); $images = array_reverse($images); sort($imagesL); $imagesL = array_reverse($imagesL); for ($a=0; $a