我想逐行读取文件,但不完全加载到内存中。

我的文件太大了,无法在内存中打开,如果尝试这样做,我总是会得到内存溢出错误。

文件大小为1gb。


当前回答

如果你想在打开一个大文件时使用foreach而不是while,你可能想在Generator中封装while循环,以避免将整个文件加载到内存中:

/**
 * @return Generator
 */
$fileData = function() {
    $file = fopen(__DIR__ . '/file.txt', 'r');

    if (!$file) {
        return; // die() is a bad practice, better to use return
    }    
    while (($line = fgets($file)) !== false) {
        yield $line;
    }

    fclose($file);
};

像这样使用它:

foreach ($fileData() as $line) {
    // $line contains current line
}

通过这种方式,您可以在foreach()中处理单个文件行。

注意:生成器需要>= PHP 5.5

其他回答

<?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++;
}
?>

对于这个问题,一个流行的解决方案是使用新的行字符。使用简单的str_replace就可以很容易地修复这个问题。

$handle = fopen("some_file.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        $line = str_replace("\n", "", $line);
    }
    fclose($handle);
}

函数读取数组返回

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;
}

如果你想在打开一个大文件时使用foreach而不是while,你可能想在Generator中封装while循环,以避免将整个文件加载到内存中:

/**
 * @return Generator
 */
$fileData = function() {
    $file = fopen(__DIR__ . '/file.txt', 'r');

    if (!$file) {
        return; // die() is a bad practice, better to use return
    }    
    while (($line = fgets($file)) !== false) {
        yield $line;
    }

    fclose($file);
};

像这样使用它:

foreach ($fileData() as $line) {
    // $line contains current line
}

通过这种方式,您可以在foreach()中处理单个文件行。

注意:生成器需要>= PHP 5.5

有一个file()函数返回文件中包含的行数组。

foreach(file('myfile.txt') as $line) {
   echo $line. "\n";
}