PHP获取txt文件指定行内容,很好用的一个方法!可获取指定文件的置顶行或者指定行返回内容长度,我是用做广告点击数据统计,省得创建数据库…
//创建一个函数【get_Line】,获取指定行内容
function get_Line($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;
}
//file_name_1 = "/cs/wj.txt";//服务器绝对路径
$file_1_1 = get_Line($file_name_1,2);//读取文件第2行内容————【文件路径$file【file_name_1】,行数$line,【2】指定行返回内容长度$length】
隐藏内容需要登录才可以看见
登录





