search
Homephp教程php手册php读取大文件最好的实现方法

php读取大文件最好的实现方法

Jun 13, 2016 am 09:28 AM
phpgenerallybigaccomplishusdocumentmethodmostread

php读取大文件最好的实现方法

   php读取大文件方法我们一般是一行行来讲取而不是一次性把文件全部写入内存中了,这样会导致php程序卡死,下面给大家整理一个例子。

 代码如下  

读取大文件最后几行数据:
/**
 * 取文件最后$n行
 * @param string $filename 文件路径
 * @param int $n 最后几行
 * @return mixed false表示有错误,成功则返回字符串
 */
function FileLastLines($filename,$n){
    if(!$fp=fopen($filename,’r')){
        echo ”打开文件失败,请检查文件路径是否正确,路径和文件名不要包含中文”;
        return false;
    }
    $pos=-2;
    $eof=”";
    $str=”";
    while($n>0){
        while($eof!=”n”){
            if(!fseek($fp,$pos,SEEK_END)){
                $eof=fgetc($fp);
                $pos–;
            }else{
                break;
            }
        }
        $str.=fgets($fp);
        $eof=”";
        $n–;
    }
    return $str;
}

echo nl2br(FileLastLines(‘sss.txt’,4));
/**  * 取文件最后$n行  * @param string $filename 文件路径  * @param int $n 最后几行  * @return mixed false表示有错误,成功则返回字符串  */ function FileLastLines($filename,$n){     if(!$fp=fopen($filename,'r')){         echo "打开文件失败,请检查文件路径是否正确,路径和文件名不要包含中文";         return false;     }     $pos=-2;     $eof="";     $str="";     while($n>0){         while($eof!="n"){             if(!fseek($fp,$pos,SEEK_END)){                 $eof=fgetc($fp);                 $pos--;             }else{                 break;             }         }         $str.=fgets($fp);         $eof="";         $n--;     }     return $str; } echo nl2br(FileLastLines('sss.txt',4));

 

function tail($fp,$n,$base=5)
{
assert($n>0);
$pos = $n+1;
$lines = array();
while(count($lines) try{
fseek($fp,-$pos,SEEK_END);
} catch (Exception $e){
fseek(0);
break;
}
$pos *= $base;
while(!feof($fp)){
array_unshift($lines,fgets($fp));
}
}
return array_slice($lines,0,$n);
}
var_dump(tail(fopen("access.log","r+"),10));
$fp = fopen($file, "r");
$line = 10;
$pos = -2;
$t = " ";
$data = "";
while ($line > 0) {
while ($t != "n") {
fseek($fp, $pos, SEEK_END);
$t = fgetc($fp);
$pos --;
}
$t = " ";
$data .= fgets($fp);
$line --;
}
fclose ($fp);
echo $data

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft