1

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?

Andy Brown
  • 11,766
  • 2
  • 42
  • 61
Ido
  • 171
  • 2
  • 13
  • Why you use 'c' as mode for fopen, it is for writing and you reading the file after it. Also you said you want to create file, but in code it looks like you read the code. – Anton M. Mar 03 '17 at 08:44
  • What I need to do is to create a Java file and then add some code into it. What argument to put instead of "c"? – Ido Mar 03 '17 at 08:46
  • With c it will create code, but what you expect from loop?? Your loop reading lines from file you just created. – Anton M. Mar 03 '17 at 08:48
  • So to delete the loop? – Ido Mar 03 '17 at 08:51
  • I don't know what you try to do =) May be you need to write some code to java file in loop. And then close file. – Anton M. Mar 03 '17 at 08:52
  • What I'm trying to do is first to create a Java file, then create an array on the PHP script with any code to write to the Java file, and then write the array contents to the Java file. What do you suggest to do? – Ido Mar 03 '17 at 09:01
  • Your code already should create file, just remove loop(it cause infinity loop). Or reverse it, to write lines from array and not read lines to array. – Anton M. Mar 03 '17 at 09:03

3 Answers3

2

Try this:

ini_set('memory_limit', '512M');
Pang
  • 9,564
  • 146
  • 81
  • 122
vignesh
  • 29
  • 2
1

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.

Axel
  • 13,939
  • 5
  • 50
  • 79
  • But the file isn't exist, it should create the file – Ido Mar 07 '17 at 07:43
  • So what are you trying to do? If you call `fgets`, you are trying to read from a file. What do you expect to read from the file if it is created by `fopen`? – Axel Mar 07 '17 at 07:50
0

I have to Get On my Drupal 7 project and I was changed to sites/default/settings.php file include bottom two line please check below

ini_set('memory_limit', '-1'); // unlimited memory limit
ini_set('max_execution_time', 3000);

enter image description here

rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38