4

i have a folder called XXX it contains .jpg .gif .txt .... etc

i want only to read .txt files how to do that ?

Thanks

1

3 Answers 3

13

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

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

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());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.