Home >> Snippet List >> Snippet

Name
Buffered Download code
Description
For downloading very large files (10 meg or more), split the file into chunks when sending
PHP Snippet
<?php
 
//start buffered download
    WHILE(!feof($fp)){
 
        $total     = filesize($file);
        $sent      = 0;
        $blocksize = (2 << 20); //2M chunks
        $handle    = fopen($file, "r");
 
        // Now we need to loop through the file 
        // AND echo out chunks of file data
        WHILE($sent < $total){
            echo fread($handle, $blocksize);
            $sent += $blocksize;
        }
 
        EXIT(0);
        @flush();
        @ob_flush();
    }
 
?>