如何计算文件夹中的文件,但排除索引.php文件不计为 (PHP)


How do I count files in a folder but exclude index.php file from being counted as (PHP)

我在解决这个问题时遇到了问题。如何从文件夹/中排除某些文件(例如:index.php)以计为。有什么建议吗?

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

我猜你也需要在某个时候列出这些文件,所以这将建立一个新的"批准"文件名数组。

$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);

你可以做这样的事情:

$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 将找到大海捞针字符串中第一次出现的针的数字位置。

获得此值后,只需从数组中取消设置该数组索引files即可。