php怎么一行一行的读取文件

php怎么一行一行的读取文件,第1张

$myfile = fopen("web.txt", "r") 

echo fgets($myfile)//fgets为读取一行,行本质是段落

fclose($myfile)

<?php

$c = getLine('./a.txt', 10) // 读取a.txt文件第10行内容

echo $c

/**

 * 获取指定行内容

 *

 * @param $file 文件路径

 * @param $line 行数

 * @param $length 指定行返回内容长度

 */

function getLine($file, $line, $length = 4096){

$returnTxt = null // 初始化返回

$i = 1 // 行数

$handle = @fopen($file, "r")

if ($handle) {

while (!feof($handle)) {

$buffer = fgets($handle, $length)

if($line == $i) $returnTxt = $buffer

$i++

}

fclose($handle)

}

return $returnTxt

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/12056709.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-20
下一篇2023-05-20

发表评论

登录后才能评论

评论列表(0条)

    保存