summaryrefslogtreecommitdiffstats
path: root/system/helpers/download_helper.php
diff options
context:
space:
mode:
Diffstat (limited to 'system/helpers/download_helper.php')
-rw-r--r--system/helpers/download_helper.php52
1 files changed, 44 insertions, 8 deletions
diff --git a/system/helpers/download_helper.php b/system/helpers/download_helper.php
index 09c4de578..31652d53e 100644
--- a/system/helpers/download_helper.php
+++ b/system/helpers/download_helper.php
@@ -1,4 +1,4 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php
/**
* CodeIgniter
*
@@ -24,6 +24,7 @@
* @since Version 1.0
* @filesource
*/
+defined('BASEPATH') OR exit('No direct script access allowed');
/**
* CodeIgniter Download Helpers
@@ -55,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';
@@ -94,28 +112,46 @@ if ( ! function_exists('force_download'))
$filename = implode('.', $x);
}
+ if ($data === NULL && ($fp = @fopen($filepath, 'rb')) === FALSE)
+ {
+ return FALSE;
+ }
+
// Clean output buffer
- ob_clean();
+ if (ob_get_level() !== 0 && @ob_end_clean() === FALSE)
+ {
+ ob_clean();
+ }
// Generate the server headers
header('Content-Type: '.$mime);
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)
{
- header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
- header('Pragma: public');
+ header('Cache-Control: no-cache, no-store, must-revalidate');
}
- else
+
+ header('Pragma: no-cache');
+
+ // 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)
{
- header('Pragma: no-cache');
+ echo $data;
}
- exit($data);
+ fclose($fp);
+ exit;
}
}