I have a folder named XXX containing .jpg.gif.txt .... etc
I only want to read .txt files, how do I do this?
thanks
You can use glob to get an array of file names that match a given pattern. Here is an example from the manual page:
glob
foreach (glob("*.txt") as $filename) { echo "$filename size " . filesize($filename) . "\n"; }
<?php foreach (glob("*.txt") as $filename) { echo "$filename size " . filesize($filename) . "\n"; } ?>
You can use GlobIterator
GlobIterator
$iterator = new GlobIterator('*.txt', FilesystemIterator::KEY_AS_FILENAME); $n = 0; foreach ( $iterator as $item ) { printf("[%d] %s\r\n", ++ $n, $iterator->key()); }