Avatar of rgb192
rgb192
Flag for United States of America asked on

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 11 bytes) in

file_get_contents(BIG_FILE);

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 11 bytes) in


should I change a variable in php.ini
PHP

Avatar of undefined
Last Comment
rgb192

8/22/2022 - Mon
Dave Baldwin

That depends.  How big a file are you trying to get?  Note that it has to fit in memory along with all the PHP code.
rgb192

ASKER
46,000 kb

I am reading the skype sql file
Dave Baldwin

Change 'memory_limit' in 'php.ini' from 128M to 256M.  You are probably reading the file in Ok but processing such a large file will take more memory.  You may have to restart Apache.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Dave Baldwin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

The line numbers matter here.  What line of the script fails?  What is the PHP code on that line?  

I am fairly certain that the 46 megabyte file is getting processed somehow and in PHP standard (not OOP) notation, PHP often makes copies of the variables to pass the copies to the functions.  PHP arrays are also amazingly large when compared to the string data.  You might want to get familiar with memory_get_usage()
rgb192

ASKER
Sorry about not providing code.


Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 11 bytes) in C:\wamp\www\test\readingsql.php on line 3

note: sql-file has no extension.  
Code runs on nusphere phpED ide, but not wamp

<?php
$filename = "C:/Users/Acer/AppData/Roaming/Skype/respondto/sql-file";
$lines = explode("\n", file_get_contents($filename));
foreach($lines as $l) {
   if (substr($l, 0, 13) == 'CREATE TABLE ') {
      echo $l . '<br>';
   }
}

Open in new window

Ray Paseur

Please see AntiPractice #9, in play here because you have two functions on line 3, so you cannot readily isolate the cause of the failure.

$lines = explode("\n", file_get_contents($filename));

But that aside, the cause of the failure is data proliferation caused by internal designs in PHP.  Instead of that, I recommend that you try this:

$str = file_get_contents($filename);

I expect that if your data set is 46 megabytes you will be able to get it into a string variable.

Also, I would recommend that you revisit this question.  I am sure that @JulianH meant well, but that is a technically incompetent solution and you should be using the regular expression on a string variable instead of trying to use strpos() on elements of an array.  There is sometimes a bit of "deep background" thinking involved in application design, and when I saw the sample data in that question, I knew instantly that any design using an array would run out of memory very, very soon.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
rgb192

ASKER
Thanks for ini change and code explaining size.