Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
Derek Allard | dbbe408 | 2007-02-13 23:46:23 +0000 | [diff] [blame] | 2 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 3 | * CodeIgniter
|
Derek Allard | dbbe408 | 2007-02-13 23:46:23 +0000 | [diff] [blame] | 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Rick Ellis | dd1e323 | 2008-09-12 23:33:20 +0000 | [diff] [blame^] | 9 | * @copyright Copyright (c) 2008, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | dbbe408 | 2007-02-13 23:46:23 +0000 | [diff] [blame] | 12 | * @since Version 1.0
|
| 13 | * @filesource
|
| 14 | */
|
| 15 |
|
| 16 | // ------------------------------------------------------------------------
|
| 17 |
|
| 18 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 19 | * CodeIgniter Download Helpers
|
Derek Allard | dbbe408 | 2007-02-13 23:46:23 +0000 | [diff] [blame] | 20 | *
|
| 21 | * @package CodeIgniter
|
| 22 | * @subpackage Helpers
|
| 23 | * @category Helpers
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 24 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 25 | * @link http://codeigniter.com/user_guide/helpers/download_helper.html
|
Derek Allard | dbbe408 | 2007-02-13 23:46:23 +0000 | [diff] [blame] | 26 | */
|
| 27 |
|
| 28 | // ------------------------------------------------------------------------
|
| 29 |
|
| 30 | /**
|
| 31 | * Force Download
|
| 32 | *
|
| 33 | * Generates headers that force a download to happen
|
| 34 | *
|
| 35 | * @access public
|
| 36 | * @param string filename
|
| 37 | * @param mixed the data to be downloaded
|
| 38 | * @return void
|
| 39 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 40 | if ( ! function_exists('force_download'))
|
Derek Allard | dbbe408 | 2007-02-13 23:46:23 +0000 | [diff] [blame] | 41 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 42 | function force_download($filename = '', $data = '')
|
Derek Allard | dbbe408 | 2007-02-13 23:46:23 +0000 | [diff] [blame] | 43 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 44 | if ($filename == '' OR $data == '')
|
| 45 | {
|
| 46 | return FALSE;
|
| 47 | }
|
Derek Allard | dbbe408 | 2007-02-13 23:46:23 +0000 | [diff] [blame] | 48 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 49 | // Try to determine if the filename includes a file extension.
|
| 50 | // We need it in order to set the MIME type
|
| 51 | if (FALSE === strpos($filename, '.'))
|
| 52 | {
|
| 53 | return FALSE;
|
| 54 | }
|
Derek Allard | dbbe408 | 2007-02-13 23:46:23 +0000 | [diff] [blame] | 55 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 56 | // Grab the file extension
|
| 57 | $x = explode('.', $filename);
|
| 58 | $extension = end($x);
|
Derek Allard | dbbe408 | 2007-02-13 23:46:23 +0000 | [diff] [blame] | 59 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 60 | // Load the mime types
|
| 61 | @include(APPPATH.'config/mimes'.EXT);
|
Derek Allard | dbbe408 | 2007-02-13 23:46:23 +0000 | [diff] [blame] | 62 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 63 | // Set a default mime if we can't find it
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 64 | if ( ! isset($mimes[$extension]))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 65 | {
|
| 66 | $mime = 'application/octet-stream';
|
| 67 | }
|
| 68 | else
|
| 69 | {
|
| 70 | $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
|
| 71 | }
|
Derek Allard | dbbe408 | 2007-02-13 23:46:23 +0000 | [diff] [blame] | 72 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 73 | // Generate the server headers
|
| 74 | if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
|
| 75 | {
|
| 76 | header('Content-Type: "'.$mime.'"');
|
| 77 | header('Content-Disposition: attachment; filename="'.$filename.'"');
|
| 78 | header('Expires: 0');
|
| 79 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
|
| 80 | header("Content-Transfer-Encoding: binary");
|
| 81 | header('Pragma: public');
|
| 82 | header("Content-Length: ".strlen($data));
|
| 83 | }
|
| 84 | else
|
| 85 | {
|
| 86 | header('Content-Type: "'.$mime.'"');
|
| 87 | header('Content-Disposition: attachment; filename="'.$filename.'"');
|
| 88 | header("Content-Transfer-Encoding: binary");
|
| 89 | header('Expires: 0');
|
| 90 | header('Pragma: no-cache');
|
| 91 | header("Content-Length: ".strlen($data));
|
| 92 | }
|
Derek Allard | dbbe408 | 2007-02-13 23:46:23 +0000 | [diff] [blame] | 93 |
|
Derek Allard | c0c5c97 | 2008-05-01 01:56:51 +0000 | [diff] [blame] | 94 | exit($data);
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 95 | }
|
Derek Allard | dbbe408 | 2007-02-13 23:46:23 +0000 | [diff] [blame] | 96 | }
|
| 97 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 98 |
|
| 99 | /* End of file download_helper.php */
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 100 | /* Location: ./system/helpers/download_helper.php */ |