简体   繁体   English

PHP_Count 给定文件夹下所有文件的总行数

[英]PHP_Count total lines in all files under a given folder

Just wanted to count total number of line from all the files from the folder.只是想计算文件夹中所有文件的总行数。 following php function helps me to count line num for only particular file.以下 php 函数可帮助我计算仅特定文件的行号。 just wondering what is the way to cont total number of lines from the folder.只是想知道从文件夹中控制总行数的方法是什么。

$lines = COUNT(FILE($file));  

Thank you.!谢谢。!

You could iterate the directory and count each file and sum them all.您可以迭代目录并计算每个文件并将它们全部相加。 And you are using file() function, which will load the whole content into memory, if the file is very large, your php script will reach the memory limit of your config.并且您正在使用file()函数,该函数会将整个内容加载到内存中,如果文件非常大,您的 php 脚本将达到您的配置的内存限制。

If you could use external command, there is a solution with one line.如果您可以使用外部命令,则可以使用一行解决方案。 (If you are using windows, just omit it.) (如果您使用的是 Windows,请忽略它。)

$total = system("find $dir_path -type f -exec wc -l {} \; | awk '{total += $1} END{print total}'");

Something like this perhaps:可能是这样的:

<?php

$line_count = 0;
if ($handle = opendir('some/dir/path')) {
    while (false !== ($entry = readdir($handle))) {
        if (is_file($entry)) {
            $line_count += count(file($entry));
        }
    }
    closedir($handle);
}

var_dump($line_count);

?>

Check out the Standard PHP Library (aka SPL) for DirectoryIterator:查看 DirectoryIterator 的标准 PHP 库(又名 SPL):

$dir = new DirectoryIterator('/path/to/dir');
foreach($dir as $file ){
  $x += (isImage($file)) ? 1 : 0;
}

(FYI there is an undocumented function called iterator_count() but probably best not to rely on it for now I would imagine. And you'd need to filter out unseen stuff like . and .. anyway.) (仅供参考,有一个名为 iterator_count() 的未记录函数,但我想现在最好不要依赖它。无论如何,您都需要过滤掉看不见的东西,例如 . 和 .. 。)

or try this:--或者试试这个:--

see url :- http://www.brightcherry.co.uk/scribbles/php-count-files-in-a-directory/见网址:- http://www.brightcherry.co.uk/scribbles/php-count-files-in-a-directory/

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

A very basic example of counting the lines might look something like the following, which gives the same numbers as xdazz's answer .计算行数的一个非常基本的示例可能如下所示,它给出的数字与xdazz 的答案相同。

<?php

$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__));

$lines = $files = 0;
foreach ($files as $fileinfo) {
    if (!$fileinfo->isFile()) {
        continue;
    }
    $files++;
    $read = $fileinfo->openFile();
    $read->setFlags(SplFileObject::READ_AHEAD);
    $lines += iterator_count($read) - 1; // -1 gives the same number as "wc -l"
}

printf("Found %d lines in %d files.", $lines, $files);

See also也可以看看

Same as one above (salathe's answer), except this one prints the number of lines (now in php7) rather than a bunch of error messages.与上面的相同(salathe 的回答),除了这个打印行数(现在在 php7 中)而不是一堆错误消息。

$files = new RecursiveIteratorIterator(new 
RecursiveDirectoryIterator(__DIR__));

$lines = 0;
foreach ($files as $fileinfo) {
    if (!$fileinfo->isFile()) {
        continue;
    }
    $read = $fileinfo->openFile();
    $read->setFlags(SplFileObject::READ_AHEAD);
    $lines += iterator_count($read) - 1; // -1 gives the same number as "wc -l"
}

echo ("Found :$lines");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM