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

Rate this post

php count lines in file – Count number of lines in a file – Write a PHP script to count number of lines in a file. The file() function actually returns an array.

php count lines in file – PHP Function to Count Lines in a Data Text File

php count lines in file – How to count the number of lines in a file using php? – To explain it we assume a file named result.txt with two lines in it.

Read Also:  jquery multiple Image Upload with preview and Delete using PHP

<?php
$file = basename($_SERVER['PHP_SELF']); 
$total_cnt = count(file($file)); 
echo "There are $total_cnt lines in $file"."\n";
?>

Free Live Chat for Any Issue

PHP – How to Calculate no of Lines in File?

<?php

function total_lines($file)
{
    $call_request = fopen( $file, "r" );

    $total_cnt = 0;

    while(fgets($call_request))
    {
        $total_cnt++;
    }

    fclose($call_request);

    return $total_cnt;
}

$file='result.txt';

$total_cnt = total_lines($file);
echo $total_cnt;

?>

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

<?php
$call_request = "pakainfo_v1.txt";
$result_lines = file($call_request);
$total_cnt= count($result_lines);
echo $total_cnt;
?>

Example

<?php
	$filePath = "pakainfo_v1.txt";
	$lines = count(file($filePath));
	echo $lines;
?>

Download

don’t Miss : Php Count Lines In File