summaryrefslogtreecommitdiffstats
path: root/system/helpers/download_helper.php
diff options
context:
space:
mode:
authorEric Roberts <eric@cryode.com>2012-12-12 14:02:11 +0100
committerEric Roberts <eric@cryode.com>2012-12-12 14:02:11 +0100
commitb9e35f21e1c70b6aa67c47e9244ed83195abc00a (patch)
tree64f82db362deeac48cc20d1d1afd80651f36f5a5 /system/helpers/download_helper.php
parent0b05705c52c3bca7f9b3aee657c888e8ad1ff422 (diff)
parent545a7c86701875e1412bcde316e9bcc76d9a23a0 (diff)
Merge branch 'refs/heads/develop' into feature/form_error_msgs
Conflicts: system/language/english/form_validation_lang.php user_guide_src/source/libraries/form_validation.rst Signed-off-by: Eric Roberts <eric@cryode.com>
Diffstat (limited to 'system/helpers/download_helper.php')
-rw-r--r--system/helpers/download_helper.php84
1 files changed, 58 insertions, 26 deletions
diff --git a/system/helpers/download_helper.php b/system/helpers/download_helper.php
index 19192a147..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
@@ -37,24 +38,41 @@
// ------------------------------------------------------------------------
-/**
- * Force Download
- *
- * Generates headers that force a download to happen
- *
- * @param string filename
- * @param mixed the data to be downloaded
- * @param bool wether to try and send the actual file MIME type
- * @return void
- */
if ( ! function_exists('force_download'))
{
+ /**
+ * Force Download
+ *
+ * Generates headers that force a download to happen
+ *
+ * @param string filename
+ * @param mixed the data to be downloaded
+ * @param bool whether to try and send the actual file MIME type
+ * @return void
+ */
function force_download($filename = '', $data = '', $set_mime = FALSE)
{
- if ($filename == '' OR $data == '')
+ if ($filename === '' OR $data === '')
{
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';
@@ -73,14 +91,7 @@ if ( ! function_exists('force_download'))
}
// Load the mime types
- if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
- {
- include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
- }
- elseif (is_file(APPPATH.'config/mimes.php'))
- {
- include(APPPATH.'config/mimes.php');
- }
+ $mimes =& get_mimes();
// Only change the default MIME if we can find one
if (isset($mimes[$extension]))
@@ -101,25 +112,46 @@ 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 && @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;
}
}