php count lines in file – How to get the number of lines in a file using PHP?

php count lines in file – Efficiently counting the number of lines of a text file. This is a PHP function that will return the number lines in a text file. Use it to count lines in a data file.

php count lines in file

PHP – How to Calculate no of Lines in File?

index.php

<?php
function count_lines($file)
{
    $handle = fopen( $file, "r" );
    $count = 0;
    while(fgets($handle))
    {
        $count++;
    }
    fclose($handle);
    return $count;
}

$file='welcome.txt';
$count = count_lines($file);
echo $count;

?>

PHP program to count the number of lines in a text file :

<?php
$my_text_file = "documents_pakainfo.txt";
$all_lines = file($my_text_file);
$number_of_lines= count($all_lines);
echo $number_of_lines;
?>

PHP Function to Count Lines in a Data Text File

Count lines a data text file

function isa_count_datafile_lines($file) {
    set_time_limit(300);
    ini_set('memory_limit', '-1');
    $arr = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $c = ( false === $arr) ? 0 : count($arr);
    set_time_limit(30);// restore to default
    ini_set('memory_limit','128M');// restore to default
    return $c;
}

php count files in directory

Also Read This ๐Ÿ‘‰   Send HTML Form Data as JSON via Ajax

Example

$file = '/tmp/product_details.txt';
$count = isa_count_datafile_lines($file);
echo 'There are ' . number_format($count) . ' lines in ' . $file;

I hope you get an idea about php count lines in file.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโ€™t forget to share.