blob: 4a17e519e43e15ba856af051e92385dfa69dcc4f [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Download Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author ExpressionEngine Dev Team
25 * @link http://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
Barry Mienydd671972010-10-04 16:33:58 +020039 */
Derek Allard2067d1a2008-11-13 22:59:24 +000040if ( ! function_exists('force_download'))
41{
42 function force_download($filename = '', $data = '')
43 {
44 if ($filename == '' OR $data == '')
45 {
46 return FALSE;
47 }
48
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 }
Barry Mienydd671972010-10-04 16:33:58 +020055
Derek Allard2067d1a2008-11-13 22:59:24 +000056 // Grab the file extension
57 $x = explode('.', $filename);
58 $extension = end($x);
59
60 // Load the mime types
61 @include(APPPATH.'config/mimes'.EXT);
Barry Mienydd671972010-10-04 16:33:58 +020062
Derek Allard2067d1a2008-11-13 22:59:24 +000063 // Set a default mime if we can't find it
64 if ( ! isset($mimes[$extension]))
65 {
66 $mime = 'application/octet-stream';
67 }
68 else
69 {
70 $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
71 }
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073 // Generate the server headers
Robin Sowell76b369e2010-03-19 11:15:28 -040074 if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000075 {
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 }
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 exit($data);
95 }
96}
97
98
99/* End of file download_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000100/* Location: ./system/helpers/download_helper.php */