计算目录php中的文件数量

Bra*_*cer 96 php directory file count

我正在开发一个稍微新的项目.我想知道如何制作它,以便计算某个目录中有多少文件.

<div id="header">
<?php 
    $dir = opendir('uploads/'); # This is the directory it will count from
    $i = 0; # Integer starts at 0 before counting

    # While false is not equal to the filedirectory
    while (false !== ($file = readdir($dir))) { 
        if (!in_array($file, array('.', '..') and !is_dir($file)) $i++;
    }

    echo "There were $i files"; # Prints out how many were in the directory
?>
</div>
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止(从搜索).但它出现不正常?我添加了一些注释,所以随意删除它们,它们只是让我尽可能地理解它.

如果您需要更多信息或觉得我没有详细描述,请随时说明.

Bab*_*aba 250

您可以简单地执行以下操作:

$fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
printf("There were %d Files", iterator_count($fi));
Run Code Online (Sandbox Code Playgroud)

  • +1因为这很可爱,但我可以想象它对于大多数PHP开发者而言相当混乱/不可读.我会选择其他答案中的一种方法. (20认同)
  • 另请注意,这仅适用于PHP> = 5.3.0 (6认同)
  • 您不需要传入标志`FilesystemIterator::SKIP_DOTS`,因为这是默认设置。 (4认同)
  • 这应该是公认的答案,除非提问者想要更多自定义版本,他们可以排除某些文件. (2认同)
  • 最短,最快,最有效的解决方案.谢谢! (2认同)

JKi*_*rtz 64

您可以像这样获取文件计数:

$directory = "/path/to/dir/";
$filecount = 0;
$files = glob($directory . "*");
if ($files){
 $filecount = count($files);
}
echo "There were $filecount files";
Run Code Online (Sandbox Code Playgroud)

其中"*"是可以更改的特定文件类型,如果你想要一个像"*.jpg",或者你可以做多种文件类型是这样的:

glob($directory . "*.{jpg,png,gif}",GLOB_BRACE)
Run Code Online (Sandbox Code Playgroud)

GLOB_BRACE标志展开{a,b,c}以匹配'a','b'或'c'

  • 真棒,这种方法有很多可能性来过滤文件以及计算它们:)创建一个简单的循环和几个条件会很棒...但是如何在该目录中包含其他目录等等来计算所有文件并从计数中排除目录? (2认同)
  • 对于此解决方案不起作用的人,请添加 `__DIR__ 。` 之前`"/path/to/dir/"` (`__DIR__ . "/path/to/dir/"`) (2认同)

Lau*_*ieu 43

你应该有 :

<div id="header">
<?php 
    // integer starts at 0 before counting
    $i = 0; 
    $dir = 'uploads/';
    if ($handle = opendir($dir)) {
        while (($file = readdir($handle)) !== false){
            if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) 
                $i++;
        }
    }
    // prints out how many were in the directory
    echo "There were $i files";
?>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 如果指出OP在简约文本中所做的差异/错误,那将是很好的(并且有帮助). (2认同)
  • 嘿,谢谢你。它仍然不会出现在我身上,但我觉得这可能与我的样式表或类似的东西有关。无论哪种方式,都非常感谢您的帮助。编辑:已修复:)非常感谢! (2认同)
  • 不要忘记关闭目录:) (2认同)

int*_*lis 41

试试这个.

// Directory
$directory = "/dir";

// Returns array of files
$files = scandir($directory);

// Count number of files and store them to variable..
$num_files = count($files)-2;
Run Code Online (Sandbox Code Playgroud)

不计算'.' 和'..'.

  • 摆脱`.`和`..`试试这个:$ files =`array_diff(scandir("/ dir"),array(".",".."));` (6认同)
  • 也许`$ num_files = count($ files) - 2;`?因为`.`以及`..` (3认同)
  • `array_slice(scandir($directory),2)` (2认同)

vbw*_*bwx 13

既然我也需要这个,我很好奇哪个选择最快.

我发现-如果你想要的是一个文件计数-巴巴的解决方案是很多比别人快.我很惊讶.

自己试试吧:

<?php
define('MYDIR', '...');

foreach (array(1, 2, 3) as $i)
{
    $t = microtime(true);
    $count = run($i);
    echo "$i: $count (".(microtime(true) - $t)." s)\n";
}

function run ($n)
{
    $func = "countFiles$n";
    $x = 0;
    for ($f = 0; $f < 5000; $f++)
        $x = $func();
    return $x;
}

function countFiles1 ()
{
    $dir = opendir(MYDIR);
    $c = 0;
    while (($file = readdir($dir)) !== false)
        if (!in_array($file, array('.', '..')))
            $c++;
    closedir($dir);
    return $c;
}

function countFiles2 ()
{
    chdir(MYDIR);
    return count(glob("*"));
}

function countFiles3 () // Fastest method
{
    $f = new FilesystemIterator(MYDIR, FilesystemIterator::SKIP_DOTS);
    return iterator_count($f);
}
?>
Run Code Online (Sandbox Code Playgroud)

测试运行:(显然,glob()不计算点文件)

1: 99 (0.4815571308136 s)
2: 98 (0.96104407310486 s)
3: 99 (0.26513481140137 s)
Run Code Online (Sandbox Code Playgroud)


Nir*_*ara 12

工作演示

<?php

$directory = "../images/team/harry/"; // dir location
if (glob($directory . "*.*") != false)
{
 $filecount = count(glob($directory . "*.*"));
 echo $filecount;
}
else
{
 echo 0;
}

?>
Run Code Online (Sandbox Code Playgroud)


小智 9

我认为最好的答案是:

$num = count(glob("/exact/path/to/files/" . "*"));
echo $num;
Run Code Online (Sandbox Code Playgroud)
  • 它不算数。和..
  • 其一班轮
  • 我为此感到骄傲


Phi*_*sen 5

我用这个:

count(glob("yourdir/*",GLOB_BRACE))
Run Code Online (Sandbox Code Playgroud)