Fastest way possible to read contents of a file

2019-01-16 14:59发布

Ok, I'm looking for the fastest possible way to read all of the contents of a file via php with a filepath on the server, also these files can be huge. So it's very important that it does a READ ONLY to it as fast as possible.

Is reading it line by line faster than reading the entire contents? Though, I remember reading up on this some, that reading the entire contents can produce errors for huge files. Is this true?

标签: php file-io
9条回答
Lonely孤独者°
2楼-- · 2019-01-16 15:29
foreach (new SplFileObject($filepath) as $lineNumber => $lineContent) {

    echo $lineNumber."==>".$lineContent;  
    //process your operations here
}
查看更多
爷的心禁止访问
3楼-- · 2019-01-16 15:29

You Could Try cURL (http://php.net/manual/en/book.curl.php).

Altho You Might Want To Check, It Has Its Limits As Well

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch); // Whole Page As String
curl_close ($ch);
查看更多
甜甜的少女心
4楼-- · 2019-01-16 15:31

Use fpassthru or readfile. Both use constant memory with increasing file size.

http://raditha.com/wiki/Readfile_vs_include

查看更多
登录 后发表回答