summaryrefslogtreecommitdiffstats
path: root/system/helpers
diff options
context:
space:
mode:
authorAndrey Andreev <narf@bofh.bg>2012-11-22 15:57:23 +0100
committerAndrey Andreev <narf@bofh.bg>2012-11-22 15:57:23 +0100
commit53fff911de3564c8688550452f138991730a704f (patch)
tree57b9845b0feba75184c2d88b35f4c8d4a8a99676 /system/helpers
parent93f5c5d2f9fc385c1617c125cfdf1822e6d7aee2 (diff)
Added support for stream-like downloads of existing files to force_download()
Based on code/ideas from PR #365, #1254
Diffstat (limited to 'system/helpers')
-rw-r--r--system/helpers/download_helper.php41
1 files changed, 38 insertions, 3 deletions
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;
}
}