blob: 2a555ae0251cb65b641ba84c467b424f3d57711d [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allarddbbe4082007-02-13 23:46:23 +00002/**
Derek Allardd2df9bc2007-04-15 17:41:17 +00003 * CodeIgniter
Derek Allarddbbe4082007-02-13 23:46:23 +00004 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Rick Ellisdd1e3232008-09-12 23:33:20 +00009 * @copyright Copyright (c) 2008, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allarddbbe4082007-02-13 23:46:23 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
Derek Allardd2df9bc2007-04-15 17:41:17 +000019 * CodeIgniter Download Helpers
Derek Allarddbbe4082007-02-13 23:46:23 +000020 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
Derek Allard3d879d52008-01-18 19:41:32 +000024 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000025 * @link http://codeigniter.com/user_guide/helpers/download_helper.html
Derek Allarddbbe4082007-02-13 23:46:23 +000026 */
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 Jones0b59f272008-05-13 04:22:33 +000040if ( ! function_exists('force_download'))
Derek Allarddbbe4082007-02-13 23:46:23 +000041{
Derek Jones269b9422008-01-28 21:00:20 +000042 function force_download($filename = '', $data = '')
Derek Allarddbbe4082007-02-13 23:46:23 +000043 {
Derek Jones269b9422008-01-28 21:00:20 +000044 if ($filename == '' OR $data == '')
45 {
46 return FALSE;
47 }
Derek Allarddbbe4082007-02-13 23:46:23 +000048
Derek Jones269b9422008-01-28 21:00:20 +000049 // 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 Allarddbbe4082007-02-13 23:46:23 +000055
Derek Jones269b9422008-01-28 21:00:20 +000056 // Grab the file extension
57 $x = explode('.', $filename);
58 $extension = end($x);
Derek Allarddbbe4082007-02-13 23:46:23 +000059
Derek Jones269b9422008-01-28 21:00:20 +000060 // Load the mime types
61 @include(APPPATH.'config/mimes'.EXT);
Derek Allarddbbe4082007-02-13 23:46:23 +000062
Derek Jones269b9422008-01-28 21:00:20 +000063 // Set a default mime if we can't find it
Derek Jones0b59f272008-05-13 04:22:33 +000064 if ( ! isset($mimes[$extension]))
Derek Jones269b9422008-01-28 21:00:20 +000065 {
66 $mime = 'application/octet-stream';
67 }
68 else
69 {
70 $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
71 }
Derek Allarddbbe4082007-02-13 23:46:23 +000072
Derek Jones269b9422008-01-28 21:00:20 +000073 // 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 Allarddbbe4082007-02-13 23:46:23 +000093
Derek Allardc0c5c972008-05-01 01:56:51 +000094 exit($data);
Derek Jones269b9422008-01-28 21:00:20 +000095 }
Derek Allarddbbe4082007-02-13 23:46:23 +000096}
97
Derek Jones0b59f272008-05-13 04:22:33 +000098
99/* End of file download_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000100/* Location: ./system/helpers/download_helper.php */