Php: how to read only txt files in a directory

I have a folder named XXX containing .jpg.gif.txt .... etc

I only want to read .txt files, how do I do this?

thanks

+4
source share
3 answers

You can use glob to get an array of file names that match a given pattern. Here is an example from the manual page:

 foreach (glob("*.txt") as $filename) { echo "$filename size " . filesize($filename) . "\n"; } 
+13
source
 <?php foreach (glob("*.txt") as $filename) { echo "$filename size " . filesize($filename) . "\n"; } ?> 
+4
source

You can use GlobIterator

 $iterator = new GlobIterator('*.txt', FilesystemIterator::KEY_AS_FILENAME); $n = 0; foreach ( $iterator as $item ) { printf("[%d] %s\r\n", ++ $n, $iterator->key()); } 
0
source

All Articles