P
P
Petro Boyko2018-04-28 11:10:38
PHP
Petro Boyko, 2018-04-28 11:10:38

How to display the number of lines of files page by page?

Hello everyone, tell me how to implement, there is, say, 100 txt. files, they contain lines of different numbers, how to display the number of lines of files page by page 5ae6044499614266592088.png.
I have already done this way, the thing is that it is displayed on all files, only one sum for the last file

$files = glob("../data/*.*");

$fnumber = '10';

foreach($files as $file){

$name[] = basename($file);

$size = round(filesize($file)/1024,2);

$count = count(file($file));

$summa += count(file($file));

}


$counts = count($files);

$pages = ceil($counts/$fnumber);

$page = $_GET['page'];  

if(!$page OR $page=='')  
{  
        $nav_min = 0;  
        $nav_max = $fnumber - 1;  
}else{  
        $nav_min = ($page * $fnumber) - $fnumber;  
        $nav_max = ($fnumber * $page) - 1;  
}  


# Вывод  
 for($i = $nav_min; $i <= $nav_max; $i++)

{ 
        echo $name[$i]; // выводится 

           echo $size;   //  НЕ выводится 

            echo $count;   //  НЕ выводится 

} 

            echo $summa;   //  выводится 



    # Навигация  

    for($fpr = '', $i =1; $i <= $pages; $i++)
    {
        echo $fpr=(($i == 1 || $i == $pages || abs($i-$page) < 2) ? ($i == $page ? "<b style='font-size: 14px'>[$i]</b> | " : ' <a href="?page='.$i.'">'.$i.'</a> | ') : (($fpr == ' ... ' || $fpr == '')? '' : ' ... '));
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yan-s, 2018-04-28
@Yan-s

If the case is on Linux and all the files are in the same folder, you can do this:

$path = 'dirname/*'; // все файлы из dirname
$result = shell_exec('wc -l ' . $path);

in $result we get a line like
10 filename1.txt
17 filename2.txt
27 total

It remains only to parse this string in a way convenient for you.
Another way: count(file($filename))
First you need to get all file paths.
It is often convenient to do this using php.net/manual/en/function.glob.php We go
through the array of paths and execute on each one count(file($filename)), saving the result to an array, summing it up in a separate variable along the way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question