admin | b8afd04 | 2006-10-02 06:46:29 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Code Igniter Download Helpers |
| 20 | * |
| 21 | * @package CodeIgniter |
| 22 | * @subpackage Helpers |
| 23 | * @category Helpers |
| 24 | * @author Rick Ellis |
| 25 | * @link http://www.codeigniter.com/user_guide/helpers/download_helper.html |
| 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 | */ |
| 40 | function force_download($filename = '', $data = '') |
| 41 | { |
| 42 | if ($filename == '' OR $data == '') |
| 43 | { |
| 44 | return FALSE; |
| 45 | } |
| 46 | |
| 47 | // Try to determine if the filename includes a file extension. |
| 48 | // We need it in order to set the MIME type |
| 49 | if (FALSE === strpos($filename, '.')) |
| 50 | { |
| 51 | return FALSE; |
| 52 | } |
| 53 | |
| 54 | // Grab the file extension |
| 55 | $x = explode('.', $filename); |
| 56 | $extension = end($x); |
| 57 | |
| 58 | // Load the mime types |
| 59 | @include(APPPATH.'config/mimes'.EXT); |
| 60 | |
| 61 | // Set a default mime if we can't find it |
| 62 | if ( ! isset($mimes[$extension])) |
| 63 | { |
| 64 | $mime = 'application/octet-stream'; |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension]; |
| 69 | } |
| 70 | |
| 71 | // Generate the server headers |
| 72 | if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) |
| 73 | { |
| 74 | header('Content-Type: "'.$mime.'"'); |
| 75 | header('Content-Disposition: inline; filename="'.$filename.'"'); |
| 76 | header('Expires: 0'); |
| 77 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
| 78 | header("Content-Transfer-Encoding: binary"); |
| 79 | header('Pragma: public'); |
| 80 | header("Content-Length: ".strlen($data)); |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | header('Content-Type: "'.$mime.'"'); |
| 85 | header('Content-Disposition: attachment; filename="'.$filename.'"'); |
| 86 | header("Content-Transfer-Encoding: binary"); |
| 87 | header('Expires: 0'); |
| 88 | header('Pragma: no-cache'); |
| 89 | header("Content-Length: ".strlen($data)); |
| 90 | } |
| 91 | |
| 92 | echo $data; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | ?> |