Memory error for json_parse with PHP

I have the following code:

<?php
$FILE="giant-data-barf.txt";

$fp = fopen($FILE,'r');

//read everything into data
$data = fread($fp, filesize($FILE));
fclose($fp);

$data_arr = json_decode($data);
var_dump($data_arr);
?>

The giant-data-barf.txt file, as its name implies, is a huge file (it’s 5.4mb right now, but can increase up to several GB).

When I execute this script, I get the following error:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) in ........./data.php on line 12

I reviewed possible solutions and saw the following:

ini_set('memory_limit','16M');

and my question is, is there a limit to how big I have to set my memory? Or is there a better way to solve this problem?

+5
source share
2 answers

THIS IS A VERY BAD IDEA , with this you need to set

ini_set('memory_limit',filesize($FILE) + SOME_OVERHEAD_AMOUNT);

. , , JSON_DECODE

, WEB- , , !!!!

JSON? , , PHP.

+8

memory_limit 100M... .

-

+2

All Articles