$images_extensions = ['txt'];
$dir = 'messages';
$f = array_slice(scandir($dir), 2);
foreach($f as $filename) {
$files = fopen($filename, 'r');
$filesname = fread($files, 10);
$file_extension = strtolower(substr($filename, strrpos($filename, '.')+1));
if (!in_array($file_extension, $images_extensions)) continue;
echo '<p>' . $filesname .'</p>';
}