How to read the number of lines of a large file in php

王林
Release: 2023-04-07 21:28:01
Original
3299 people have browsed it

How to read the number of lines of a large file in php

Function introduction:

resource fopen($path, $math) Open a file, $path is the path, $math is the method, Returns a resource handle.

string fgets(resource $handle[,int $length])Read a line from the file pointer, $handle is the resource handle, and return a line of text content.

bool feof(resource $handle)Function detects whether the end of the file has been reached.

Free video tutorial recommendation: php video tutorial

ceshi.txt file content

111111111111111
22222222222222
33333333333333
44444444444444
55555555555555
66666666666666
Copy after login

The example code is as follows:

$handle = fopen('./ceshi.txt',"r");//以只读方式打开一个文件
$i = 0;
while(!feof($handle)){//函数检测是否已到达文件末尾 
    if(fgets($handle)){// 从文件指针中读取一行
        $i++;
    };
}
echo $i;//6
fclose($handle);
Copy after login

Related Recommended article tutorials: php tutorial

The above is the detailed content of How to read the number of lines of a large file in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template