fopen吃内存吗 php,file_get_contents => PHP致命错误:允许的内存耗尽

我没有处理大文件的经验,所以我不确定该怎么办。我试图使用 file_get_contents 读取几个大文件;任务是使用

preg_replace() 清洁和修补它们。

我的代码在小文件上运行良好;但是,大文件(40 MB)触发内存耗尽错误:

PHP Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 41390283 bytes)

我当时在考虑使用 fread(), 但是我不确定那也行。有解决此问题的方法吗?

感谢您的输入。

这是我的代码:

error_reporting(E_ALL);

##get find() results and remove DOS carriage returns.

##The error is thrown on the next line for large files!

$myData = file_get_contents("tmp11");

$newData = str_replace("^M", "", $myData);

##cleanup Model-Manufacturer field.

$pattern = '/(Model-Manufacturer:)(\n)(\w+)/i';

$replacement = '$1$3';

$newData = preg_replace($pattern, $replacement, $newData);

##cleanup Test_Version field and create comma delimited layout.

$pattern = '/(Test_Version=)(\d).(\d).(\d)(\n+)/';

$replacement = '$1$2.$3.$4 ';

$newData = preg_replace($pattern, $replacement, $newData);

##cleanup occasional empty Model-Manufacturer field.

$pattern = '/(Test_Version=)(\d).(\d).(\d) (Test_Version=)/';

$replacement = '$1$2.$3.$4 Model-Manufacturer:N/A--$5';

$newData = preg_replace($pattern, $replacement, $newData);

##fix occasional Model-Manufacturer being incorrectly wrapped.

$newData = str_replace("--","\n",$newData);

##fix 'Binary file' message when find() utility cannot id file.

$pattern = '/(Binary file).*/';

$replacement = '';

$newData = preg_replace($pattern, $replacement, $newData);

$newData = removeEmptyLines($newData);

##replace colon with equal sign

$newData = str_replace("Model-Manufacturer:","Model-Manufacturer=",$newData);

##file stuff

$fh2 = fopen("tmp2","w");

fwrite($fh2, $newData);

fclose($fh2);

### Functions.

##Data cleanup

function removeEmptyLines($string)

{

return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $string);

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值