Jump to content

[SOLVED] Grabbing amount of txt files in a directory


MasterACE14

Recommended Posts

Simply loop over the directory and check if the file meet your requirements and if it does add 1 to a counter:

 

<?php

$num_of_text_files = 0;

if ($handle = opendir('/path/to/files')) 

    while (false !== ($file = readdir($handle))) {
        if(preg_match("/\.txt/$", $file)) {
            $num_of_txt_files++;
        }
    }

    closedir($handle);
}

echo "There is $num_of_text_files files with the extension .txt in the folder";

?>

 

Of course you can do your "testing" different.... preg_match just seemed easiest for me to write :)

 

 

Edit: Corrected a weird sentence and I missed a ) in an if-statement

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.