Chilarai
Chilarai

Reputation: 1888

PHP Memory access

Can we access some of the system's heap memory with PHP like in C/C++? Actually, I keep getting Fatal error: Allowed memory size of 134217728 bytes exhausted while trying to do some big file operation in PHP.

I know we can tweak this limit in Apache2 config. But, for processing large unknown sized files and data can we have some kind of access to the heap to process & save the file? Also, if yes, then is there a mechanism to clear the memory after usage?

Sample code

<?php
    $filename = "a.csv";
    $handle = fopen($filename, "r");
    $contents = fread($handle, filesize($filename));
    echo $contents;
    fclose($handle);
?>

Here, a.csv is a 80mb file. Can there be a heap operation using some sort of pointer?

Upvotes: 1

Views: 189

Answers (1)

jibsteroos
jibsteroos

Reputation: 1391

Have you tried reading the file in chunks, e.g.:

<?php
$chunk = 256 * 256; // set chunk size to your liking
$filename = "a.csv";
$handle = fopen($filename, 'rb');
while (!feof($handle))
{
    $data = fread($handle, $chunk);
    echo $data;
    ob_flush();
    flush();
}
fclose($handle);

Upvotes: 1

Related Questions