为了账号安全,请及时绑定邮箱和手机立即绑定

计算目录php中有多少个文件

计算目录php中有多少个文件

PHP
慕的地10843 2019-11-11 14:51:20
我正在做一个新项目。我想知道如何制作它,以便计算某个目录中有多少个文件。<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>这是我到目前为止(从搜索中获得的)。但是它显示不正确吗?我添加了一些注释,以便随时删除它们,它们是为了使我能尽我所能。如果您需要更多信息,或者觉得我还没有足够描述,请随时声明。
查看完整描述

3 回答

?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

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


$fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);

printf("There were %d Files", iterator_count($fi));


查看完整回答
反对 回复 2019-11-11
?
慕森卡

TA贡献1806条经验 获得超8个赞

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


$directory = "/path/to/dir/";

$filecount = 0;

$files = glob($directory . "*");

if ($files){

 $filecount = count($files);

}

echo "There were $filecount files";

"*"您可以在哪里将其更改为特定的文件类型,"*.jpg"也可以执行以下多种文件类型:


glob($directory . "*.{jpg,png,gif}",GLOB_BRACE)

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


查看完整回答
反对 回复 2019-11-11
?
湖上湖

TA贡献2003条经验 获得超2个赞

你应该有 :


<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>


查看完整回答
反对 回复 2019-11-11
  • 3 回答
  • 0 关注
  • 477 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信