首页 技术 正文
技术 2022年11月8日
0 收藏 440 点赞 1,647 浏览 1704 个字
function readMaxFile($fp , $start = 0)
{
$tag = "\n";
$i = 0;
$content = '';
while($i < 20)
{
if (feof($fp))
{
return 0;
}
fseek($fp, $start, SEEK_SET);
$res = fread($fp, 1);
$content .= $res;
if (substr($content, -strlen($tag)) == $tag)
{
$i++;
echo $i." ->+++这里我插入到数据库+++"."\n";
}
$start+=1; if (feof($fp))
{
return 0;
}
}
sleep(3);
echo "从位置".$start."开始读取";
return $start;
}
$fp = fopen("install.log", "r+"); $re = readMaxFile($fp, 0);
for($i=0; $i<100; $i++)
{
if ($re==0)
{
echo 'a函数返回0了循环结束';
break;
}
$re = readMaxFile($fp, $re);
}
fclose($fp);
echo "程序结束";//下面两个从网上找的启发很大感谢分享foreach( glob( ngx_log. "/*.log" ) as $file ) {
    $log = new SplFileObject($file);//返回对象
    foreach( $log as $line ){ //$line 是一行一行的内容
        $ipnum  = getip($line);//这里调用自定义函数
        if( $ipnum ) {
            $ips[$ipnum] = (int)$ips[$ipnum] + 1;
        }
    }
} /** 返回文件从X行到Y行的内容(支持php5、php4) 
 * @param string $filename 文件名
 * @param int $startLine 开始的行数
 * @param int $endLine 结束的行数
 * @return string
 */
function getFileLines($filename, $startLine = 1, $endLine=50, $method='rb') {
    $content = array();
    $count = $endLine - $startLine; 
    // 判断php版本(因为要用到SplFileObject,PHP>=5.1.0)
    if(version_compare(PHP_VERSION, '5.1.0', '>=')){
        $fp = new SplFileObject($filename, $method);
        $fp->seek($startLine-1);// 转到第N行, seek方法参数从0开始计数
        for($i = 0; $i <= $count; ++$i) {
            $content[]=$fp->current();// current()获取当前行内容
            $fp->next();// 下一行
        }
    }else{//PHP<5.1
        $fp = fopen($filename, $method);
        if(!$fp) return 'error:can not read file';
        for ($i=1;$i<$startLine;++$i) {// 跳过前$startLine行
            fgets($fp);
        }
        for($i;$i<=$endLine;++$i){
            $content[]=fgets($fp);// 读取文件行内容
        }
        fclose($fp);
    }
    return array_filter($content); // array_filter过滤:false,null,''
}echo '<pre>';
var_dump(getFileLines('test.php',35270,35280));
echo '</pre>'; 
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295