I am trying to import a JSON file . My system Ram is 8GB , and memory_limit
is -1 . Still i am getting the error
Fatal error: Allowed memory size of 385875968 bytes exhausted (tried to allocate 190844952 bytes) in D:\xampp\htdocs\Builder_Ux\wp-content\uploads\builderux\2ae8cd02-24d6-4979-af0b-fddd322ffef9-1\text.php on line 20
I have also tried to use streamer to read the data in chunks. But as data is json so i can decode the json only after reading the full file . Here is my code
$files=array('/phaseplanoption.json');
foreach($files as $filename){
$filepath=__DIR__ .$filename;
$parser=new Bx_JSON_Parser;
$jsondata=$parser->parse($filepath);
$arraydata=json_decode($jsondata,true);
//echo $content=file_get_contents($filename);
//$content=json_decode($content,true);
echo count($arraydata['d']); echo "<br>";
}
class Bx_JSON_Parser
{
public $jsoncontent='';
public function parse($filename){
$success = $this->file_get_contents_chunked($filename,4096,function($chunk,&$handle,$iteration){
$this->jsoncontent=$this->jsoncontent.$chunk; // this is the line no 20
/*
* Do what you will with the {&chunk} here
* {$handle} is passed in case you want to seek
** to different parts of the file
* {$iteration} is the section fo the file that has been read so
* ($i * 4096) is your current offset within the file.
*/
});
return $this->jsoncontent;
}
public function file_get_contents_chunked($file,$chunk_size,$callback)
{
try
{
$handle = fopen($file, "r");
$i = 0;
while (!feof($handle))
{
call_user_func_array($callback,array(fread($handle,$chunk_size),&$handle,$i));
$i++;
}
fclose($handle);
}
catch(Exception $e)
{
trigger_error("file_get_contents_chunked::" . $e->getMessage(),E_USER_NOTICE);
return false;
}
return true;
}
}
error is on line no 20 and line no 20 is $this->jsoncontent=$this->jsoncontent.$chunk;
I know its because I am assigning all the content to the variable. But Can someone please tell me what is the better way to do this ?
File size is 623 MB