If you think that this will help to avoid memory errors, you are mistaken. Your code ( bytes_format ):
<?php
$url = 'http://speedtest.netcologne.de/test_10mb.bin';
echo 'Before: ' . bytes_format(memory_get_usage()) . PHP_EOL;
preg_match('~~', file_get_contents($url), $matches);
echo 'After: ' . bytes_format(memory_get_usage()) . PHP_EOL;
echo 'Peak: ' . bytes_format(memory_get_peak_usage(true)) . PHP_EOL;
?>
uses 10.5 MB:
Before: 215.41 KB
After: 218.41 KB
Peak: 10.5 MB
and this code:
<?php
$url = 'http://speedtest.netcologne.de/test_10mb.bin';
echo 'Before: ' . bytes_format(memory_get_usage()) . PHP_EOL;
$contents = file_get_contents($url);
preg_match('~~', $contents, $matches);
unset($contents);
echo 'After: ' . bytes_format(memory_get_usage()) . PHP_EOL;
echo 'Peak: ' . bytes_format(memory_get_peak_usage(true)) . PHP_EOL;
?>
also uses 10.5 MB:
Before: 215.13 KB
After: 217.64 KB
Peak: 10.5 MB
If you like to protect your script, you need to use the parameter $length
or read the file in pieces .
mgutt source
share