Most sites have a download function that allows them to track downloads, but for sites with output buffering activated this can be a pain

The problem

You have output buffering activated and want do present the user with a large file to download (ex: 300mb pdf), and even using fread to read 4 mb each time php throws the Allowed memory size of ??? bytes exhausted (tried to allocate ??? bytes) error

actually the problem is that php only releases memory when the script is ended

The solution

Issue an ob_flush after each read. ob_flush then cleans up the output buffer and frees apache and php memory

$handle = fopen ('large_file.iso', 'r');
while (! feof($handle)){
echo fread($handle, 4096);
ob_flush(); //flushing data to client, thus freeing memory