我想逐行读取文件,但不完全加载到内存中。
我的文件太大了,无法在内存中打开,如果尝试这样做,我总是会得到内存溢出错误。
文件大小为1gb。
我想逐行读取文件,但不完全加载到内存中。
我的文件太大了,无法在内存中打开,如果尝试这样做,我总是会得到内存溢出错误。
文件大小为1gb。
当前回答
SplFileObject在处理大文件时很有用。
function parse_file($filename)
{
try {
$file = new SplFileObject($filename);
} catch (LogicException $exception) {
die('SplFileObject : '.$exception->getMessage());
}
while ($file->valid()) {
$line = $file->fgets();
//do something with $line
}
//don't forget to free the file handle.
$file = null;
}
其他回答
<?php
echo '<meta charset="utf-8">';
$k= 1;
$f= 1;
$fp = fopen("texttranslate.txt", "r");
while(!feof($fp)) {
$contents = '';
for($i=1;$i<=1500;$i++){
echo $k.' -- '. fgets($fp) .'<br>';$k++;
$contents .= fgets($fp);
}
echo '<hr>';
file_put_contents('Split/new_file_'.$f.'.txt', $contents);$f++;
}
?>
小心使用'while(!feof……Fgets()的东西,Fgets可以得到一个错误(返回false)和永远循环而不到达文件的结束。Codaddict是最接近正确的,但当你的'while fgets'循环结束时,检查feof;如果不是真的,那么你就出错了。
函数读取数组返回
function read_file($filename = ''){
$buffer = array();
$source_file = fopen( $filename, "r" ) or die("Couldn't open $filename");
while (!feof($source_file)) {
$buffer[] = fread($source_file, 4096); // use a buffer of 4KB
}
return $buffer;
}
你可以使用fgets()函数逐行读取文件:
$handle = fopen("inputfile.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
// process the line read.
}
fclose($handle);
}
你可以为文件使用一个面向对象的接口类——SplFileObject http://php.net/manual/en/splfileobject.fgets.php (PHP 5 >= 5.1.0)
<?php
$file = new SplFileObject("file.txt");
// Loop until we reach the end of the file.
while (!$file->eof()) {
// Echo one line from the file.
echo $file->fgets();
}
// Unset the file to call __destruct(), closing the file handle.
$file = null;