You need glob() .
$compartment = "2"; $files = glob("/path/to/files/$compartment.*"); // Will find 2.txt, 2.php, 2.gif // Process through each file in the list // and output its extension if (count($files) > 0) foreach ($files as $file) { $info = pathinfo($file); echo "File found: extension ".$info["extension"]."<br>"; } else echo "No file name exists called $compartment. Regardless of extension."
By the way, what you do above is crying for the loop. Don 'repeat your code blocks, but wrap them in one of them:
$compartments = array(1, 3, 6, 9); // or whichever compartments // you wish to run through foreach ($compartments as $compartment) { ..... insert code here ....... }
source share