Search code examples
phpmemory-managementprogrammatically-createdarray-push

Allowed memory size of 134217728 bytes exhausted (tried to allocate 24 bytes)


I'm trying to create a Java file using a PHP script, but for some reason, whenever I run this script I get the following error on the screen:

Fatal error: Allowed memory of 134217728 bytes exhausted (tried to allocate 24 bytes) in C:\filepath\file.php on line 88

This is the code block with line 88:

$var1 = explode(".", $filenamee);
$var2 = explode(".", $folder);

$filename = "extfiles/".$var2[0]."/".$var1[0].".java";
$file = fopen($filename, "c");
$lines_array = array();
while (!feof($file)) {


    $line = fgets($file);
    array_push($lines_array, $line); // This is line 88

}

I tried to change the memory limit in php.ini from 128MB to 512MB, but that didn't work. I also tried to set memory limit on the code by using this:

ini_set('memory_limit', '512MB');

But that didn't work either.

What can I do to fix this?


Solution

  • Please read the documentation:

    Warning

    If the passed file pointer is not valid you may get an infinite loop, because feof() fails to return TRUE.

    Make sure your file pointer is valid, and pass "r" (= reading) for mode.