带有f_open的PHP管道数据

问题描述 投票:-1回答:1

我有一段代码,问题是文件“数据”超过8GB。这是非常占用内存的。我想减少RAM的使用,并看到f_load会是理想的,但是,我怎么会爆炸这些数据?

这是我当前的代码:

$data = file_get_contents("data");
$data = explode("|", $data);
foreach ($data as $d) { // rest of code

从理论上讲,我需要打开管道,流式处理并关闭管道。我将如何处理?

我尝试使用f_open而不是file_get_contents,但是错误开始出现,所以我做错了事情,真的很想学习。

php
1个回答
0
投票

您可以使用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
© www.soinside.com 2019 - 2024. All rights reserved.