0

I am decoding JSON files using the following:

$filename = 'file.json';
$json = file_get_contents($filename);
$data = json_decode ($json,true);

This works perfectly fine with files that are not large. It also works perfectly fine in XAMMP json_decode.

Small files work fine, but as soon as they get larger (few MB+), the HTTP 500 error kicks in.

$filename loads fine and completely in PHP, so that is not the issue.

Anyone any idea if this is an limitation of JSON handling large files or a server setting (put memory and time limits already to -1 and 0 in PHP).

Thanks!

behgez
  • 11
  • 2
  • 3
    what does the PHP error log say? – fantaghirocco Aug 03 '16 at 13:54
  • 1
    If you already amended memory limits I would guess you have chnaged the WRONG `php.ini` file. There are 2 one in the `apache\bin\` folder (affects PHP under apache) and one in the `php` folder (affects PHP CLI mode only). – RiggsFolly Aug 03 '16 at 13:56
  • Possible duplicate of [Is there a limit on how much JSON can hold?](http://stackoverflow.com/questions/1262376/is-there-a-limit-on-how-much-json-can-hold) – Amit Ray Aug 03 '16 at 13:58
  • There is also a time limit in PHP, try to call `set_time_limit(0)` before your `json_decode`. – Florent Descroix Aug 03 '16 at 13:59
  • The JSON was created by another site, filesize should not be the issue (max 22MB). It fails even with a 9MB file. So not a duplicate question. – behgez Aug 03 '16 at 15:15
  • I cannot find the 'other' php.ini file...also no log file seems to be created. – behgez Aug 03 '16 at 15:18

2 Answers2

0

The first thing that would most likely cause this is PHP is running out of memory.

Check your php.ini under the memory_limit setting and increase it.

Also you will need to check your max_execution_time to make sure that it is not timing out.

One last thing before your json_encode statement: set_time_limit(0);

Also I would suggest using a package such as JMS/Serializer

Joshua Lawson
  • 376
  • 3
  • 15
0

Increasing server memory to 1GB solved it.

behgez
  • 11
  • 2