Skip to content

Commit

Permalink
Fix #11362: Use readfile instead of custom file_send_chunk
Browse files Browse the repository at this point in the history
PHP's inbuilt readfile() function already does its own internal
chunking. We should use this function instead of our own file chunk
reading function to improve reliability and performance.
  • Loading branch information
davidhicks committed Mar 31, 2010
1 parent 482a493 commit f845ed5
Showing 1 changed file with 1 addition and 37 deletions.
38 changes: 1 addition & 37 deletions file_download.php
Expand Up @@ -155,7 +155,7 @@
}

header( 'Content-Type: ' . $t_content_type );
file_send_chunk( $t_local_disk_file );
readfile( $t_local_disk_file );
}
break;
case FTP:
Expand Down Expand Up @@ -190,39 +190,3 @@
header( 'Content-Type: ' . $t_content_type );
echo $v_content;
}
exit();

function file_send_chunk($filename, $start = 0, $maxlength = 0 ) {
static $s_safe_mode = null;
$chunksize = 4*131072;
$buffer = '';
$offset = $start;

if( $s_safe_mode == null ) {
$s_safe_mode = ini_get('safe_mode');
}

while (true) {
if ( $s_safe_mode == false) {
@set_time_limit(60*60); //reset time limit to 60 min - should be enough for 1 MB chunk
}
$buffer = file_get_contents($filename, 0, null, $offset, ( ($maxlength > 0 && $maxlength < $chunksize) ? $maxlength : $chunksize ) );
if ( $buffer === false ) {
if( $maxlength > 0 ) {
$buffer = file_get_contents($filename, 0, null, $offset, $maxlength );
} else {
$buffer = file_get_contents($filename, 0, null, $offset );
}
echo $buffer;
flush();
exit(); // end of file
}
echo $buffer;
flush();
$offset += $chunksize;
$maxlength -= $chunksize;
unset($buffer);
$buffer = null;
}
return;
}

0 comments on commit f845ed5

Please sign in to comment.