WC_Download_Handler - readfile_chunked refactor

This commit is contained in:
Mike Jolley 2014-10-24 17:06:30 +01:00
parent 1ffa095ef1
commit 3aaa397361
2 changed files with 20 additions and 38 deletions

View File

@ -321,51 +321,33 @@ class WC_Download_Handler {
/** /**
* readfile_chunked * readfile_chunked
*
* Reads file in chunks so big downloads are possible without changing PHP.INI - http://codeigniter.com/wiki/Download_helper_for_large_files/ * Reads file in chunks so big downloads are possible without changing PHP.INI - http://codeigniter.com/wiki/Download_helper_for_large_files/
* @param string $file *
* @param bool $retbytes return bytes of file * @param string $file
* @return bool|int * @return bool Success or fail
* @todo Meaning of the return value? Last return is status of fclose?
*/ */
public static function readfile_chunked( $file, $retbytes = true ) { public static function readfile_chunked( $file ) {
$chunksize = 1 * ( 1024 * 1024 ); $chunksize = 1 * ( 1024 * 1024 );
$buffer = '';
$cnt = 0;
if ( file_exists( $file ) ) { if ( file_exists( $file ) || ( version_compare( PHP_VERSION, '5.4.0', '<' ) && ini_get( 'safe_mode' ) ) ) {
$handle = fopen( $file, 'r' ); if ( ( $handle = @fopen( $file, 'r' ) ) === false ) {
if ( $handle === FALSE ) { return false;
return FALSE;
} }
} elseif ( version_compare( PHP_VERSION, '5.4.0', '<' ) && ini_get( 'safe_mode' ) ) {
$handle = @fopen( $file, 'r' ); while ( ! feof( $handle ) ) {
if ( $handle === FALSE ) { $buffer = fread( $handle, $chunksize );
return FALSE; echo $buffer;
if ( ob_get_length() ) {
ob_flush();
flush();
}
} }
} else {
return FALSE; return fclose( $handle );
} }
while ( ! feof( $handle ) ) { return false;
$buffer = fread( $handle, $chunksize );
echo $buffer;
if ( ob_get_length() ) {
ob_flush();
flush();
}
if ( $retbytes ) {
$cnt += strlen( $buffer );
}
}
$status = fclose( $handle );
if ( $retbytes && $status ) {
return $cnt;
}
return $status;
} }
} }

View File

@ -57,7 +57,7 @@ function woocommerce_create_page( $slug, $option = '', $page_title = '', $page_c
*/ */
function woocommerce_readfile_chunked( $file, $retbytes = true ) { function woocommerce_readfile_chunked( $file, $retbytes = true ) {
_deprecated_function( 'woocommerce_readfile_chunked', '2.1', 'WC_Download_Handler::readfile_chunked()' ); _deprecated_function( 'woocommerce_readfile_chunked', '2.1', 'WC_Download_Handler::readfile_chunked()' );
return WC_Download_Handler::readfile_chunked( $file, $retbytes ); return WC_Download_Handler::readfile_chunked( $file );
} }
/** /**