Buffered Download code

Submitted by:Andery Smith

Date added:24 June, 2014

Category:PHP

For downloading very large files (10 meg or more), split the file into chunks when sending.

Tags: download buffering , split file

Code 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();
}

?>
 
 

Comments