/    Sign up×
Community /Pin to ProfileBookmark

Avoid php "hanging" page

Hello,
I have a php page that the user enters some data in a textarea, and then this data is fed into a command line program. When the execution of the command line programm finishes, the results are stored in an output file that is created. The “results” php page then reads this file and the user views the results. My problem is that sometimes (depending on the amount of data inserted and, subsequently the size of the output file created by the command-line programm), the php results page crushes, outputting error like:

[code]
Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 846269 bytes) in /results.php on line 125
[/code]

In this line of the script, I read the whole file into a string and then separate each result chunk by a separator (//) that the command line programm creates:

[code]
$res_handle= fopen($output_file, “r”);
$results = fread($res_handle, filesize($output_file));
fclose($res_handle);

$separated_entries = explode (“//”, $results);
[/code]

Then, foreach chunk, I output the results to the user. Is there something that I must configure in php so that it won’t have memory problems? Or another way to slurp the total output file without exhausting the memory?

thank you!

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@SrWebDeveloperNov 13.2009 — Might want to try something like this (modifying your code as posted):

[code=php]
$res_handle= fopen($output_file, "r");
$buffer='';
while(!feof($res_handle)) {
$buffer .= fread($res_handle,8192);
}
fclose($res_handle);
$separated_entries = explode ("//", $buffer);[/code]
This way you're opening about 8k chunks at a time in the file read, which works much better when dealing with large files.

-jim
×

Success!

Help @ktsirig spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...