I’m trying to create an array from a file using PHP’s unpack function. The problem is that PHP runs out of memory when working with bigger files. The script should handle files between 3 and 4 MB when done, but still stay reasonably fast.
Here’s the basic idea:
<?php
$file = 'uploads/file.pcg';
$array = unpack('C*', file_get_contents($file));
?>
Is there a way of producing the array from the entire file at once without overloading PHP , or is my only option to work with a reasonable amount of data per script instance?
– About 1 MB seems to be reasonably fast.
– Could it be that even the array alone would need more memory than the allowed limit?
Also… Sorry if something similar has already been posted here – I don’t think it was, though. 😀
Thank you for the help.
Looks like you must increase memory_limit in your ini file as file_get_contents will load whole file into a memory. Basically, if you want to get a large array – you must do that anyway. Or you may look for other way to read file and unpack it step by step, without reading whole file into a memory.