0

I'm having issues on figuring this out. How do I exclude some files (eg: index.php) from the folder/ to be counted as. Any suggestions?

<?php 

    $dir = "folder/";
    $count = 0;
    $files = glob($dir . "*.{php}",GLOB_BRACE);
    if($files){$count = count($files);}
    echo'You have: '.$count.' files.';

?>
M50Scripts
  • 38
  • 7
  • I would have to agree that it's a duplicate, if not it would still be easily applied to what he's wanting to do. – arleslie Jun 19 '15 at 14:28

2 Answers2

0

You can do something like this:

$dir = "/folder";
$count = 0;
$files = glob($dir . "*.{php}",GLOB_BRACE);

for($i=0;$i<count($files);$i++){
    if(strpos($files[$i], "index.php")){
        unset($files[$i]);
    }
}

if($files){$count = count($files);}
echo'You have: '.$count.' files.';

strpos will Find the numeric position of the first occurrence of needle in the haystack string.

After getting this just unset that array index from files array.

RNK
  • 5,582
  • 11
  • 65
  • 133
0

I'm guessing you will need to list these files at some point as well, so this will build a new array of "approved" file names.

$dir = "";
$files = glob($dir . "*.{php}",GLOB_BRACE);
$realfiles = array();
$ignore = array('index.php','otherfile.pdf'); // List of ignored file names
foreach($files as $f) {
    if(!in_array($f, $ignore)) { // This file is not in our ignore list
        $realfiles[]=$f; // Add this file name to our realfiles array
    }
}
echo 'You have: '.count($realfiles).' files.';
print_r($realfiles);
Lin Meyer
  • 712
  • 5
  • 19
  • Thanks for your helpful information's. This also helped me but the above answer was the one that helped me further :) thanks for your time though!-bless you – M50Scripts Jun 19 '15 at 14:44