我有一段代码,问题是文件“数据”超过8GB。这是非常占用内存的。我想减少RAM的使用,并看到f_load
会是理想的,但是,我怎么会爆炸这些数据?
这是我当前的代码:
$data = file_get_contents("data");
$data = explode("|", $data);
foreach ($data as $d) { // rest of code
从理论上讲,我需要打开管道,流式处理并关闭管道。我将如何处理?
我尝试使用f_open
而不是file_get_contents
,但是错误开始出现,所以我做错了事情,真的很想学习。
您可以使用stream_get_line
读取每个块的数据块(以|
作为分隔符:PHP stream_get_line()
$fh = fopen('data', 'r'); // open the file in read-only mode
while( $d = stream_get_line($fh, 1000, '|') ) // read the file until the next |
{
echo $d . PHP_EOL ; // display one block of data
}
fclose($fh); // close the file