$photo_location = 'img/'; $unsorted = scandir($photo_location); foreach ($i = 2; $i < (count($unsorted)-1); $i++) { if (ereg("^field", $unsorted[$i])) $files .= $unsorted[$i]; } print_r $files;