From 53fff911de3564c8688550452f138991730a704f Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 22 Nov 2012 16:57:23 +0200 Subject: Added support for stream-like downloads of existing files to force_download() Based on code/ideas from PR #365, #1254 --- system/helpers/download_helper.php | 41 +++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'system/helpers/download_helper.php') diff --git a/system/helpers/download_helper.php b/system/helpers/download_helper.php index 8fe66e222..31652d53e 100644 --- a/system/helpers/download_helper.php +++ b/system/helpers/download_helper.php @@ -56,6 +56,23 @@ if ( ! function_exists('force_download')) { return FALSE; } + elseif ($data === NULL) + { + if (@is_file($filename) && @file_exists($filename) && ($filesize = @filesize($filename)) !== FALSE) + { + $filepath = $filename; + $filename = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $filename)); + $filename = end($filename); + } + else + { + return FALSE; + } + } + else + { + $filesize = strlen($data); + } // Set the default MIME type to send $mime = 'application/octet-stream'; @@ -95,8 +112,13 @@ if ( ! function_exists('force_download')) $filename = implode('.', $x); } + if ($data === NULL && ($fp = @fopen($filepath, 'rb')) === FALSE) + { + return FALSE; + } + // Clean output buffer - if (ob_get_level() !== 0) + if (ob_get_level() !== 0 && @ob_end_clean() === FALSE) { ob_clean(); } @@ -106,7 +128,7 @@ if ( ! function_exists('force_download')) header('Content-Disposition: attachment; filename="'.$filename.'"'); header('Expires: 0'); header('Content-Transfer-Encoding: binary'); - header('Content-Length: '.strlen($data)); + header('Content-Length: '.$filesize); // Internet Explorer-specific headers if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) @@ -116,7 +138,20 @@ if ( ! function_exists('force_download')) header('Pragma: no-cache'); - exit($data); + // If we have raw data - just dump it + if ($data !== NULL) + { + exit($data); + } + + // Flush 1MB chunks of data + while ( ! feof($fp) && ($data = fread($fp, 1048576)) !== FALSE) + { + echo $data; + } + + fclose($fp); + exit; } } -- cgit v1.2.3-24-g4f1b