blob: f3f0ff2cacc4a67e2934c1fff9555eca7c7c957e [file] [log] [blame]
Andrey Andreevb38c5dd2012-02-29 14:07:45 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevb38c5dd2012-02-29 14:07:45 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter Download Helpers
30 *
31 * @package CodeIgniter
32 * @subpackage Helpers
33 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/helpers/download_helper.html
36 */
37
38// ------------------------------------------------------------------------
39
40/**
41 * Force Download
42 *
43 * Generates headers that force a download to happen
44 *
45 * @access public
46 * @param string filename
47 * @param mixed the data to be downloaded
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020048 * @param bool wether to try and send the actual file MIME type
Derek Allard2067d1a2008-11-13 22:59:24 +000049 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020050 */
Derek Allard2067d1a2008-11-13 22:59:24 +000051if ( ! function_exists('force_download'))
52{
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020053 function force_download($filename = '', $data = '', $set_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000054 {
55 if ($filename == '' OR $data == '')
56 {
57 return FALSE;
58 }
59
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020060 // Set the default MIME type to send
61 $mime = 'application/octet-stream';
62
63 if ($set_mime === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +000064 {
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020065 /* If we're going to detect the MIME type,
66 * we'll need a file extension.
67 */
68 if (FALSE === strpos($filename, '.'))
69 {
70 return FALSE;
71 }
72
Andrey Andreevdb0c0622012-02-29 19:02:46 +020073 $extension = explode('.', $filename);
74 $extension = end($extension);
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020075
76 // Load the mime types
77 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
78 {
79 include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
80 }
81 elseif (is_file(APPPATH.'config/mimes.php'))
82 {
83 include(APPPATH.'config/mimes.php');
84 }
85
86 // Only change the default MIME if we can find one
87 if (isset($mimes[$extension]))
88 {
89 $mime = is_array($mimes[$extension]) ? $mimes[$extension][0] : $mimes[$extension];
90 }
Derek Allard2067d1a2008-11-13 22:59:24 +000091 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Allard2067d1a2008-11-13 22:59:24 +000093 // Generate the server headers
dododedodonl4da94782012-03-10 13:56:17 +010094 header('Content-Type: '.$mime);
Eric Roberts0e4d2b62012-01-23 18:19:20 -060095 header('Content-Disposition: attachment; filename="'.$filename.'"');
96 header('Expires: 0');
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020097 header('Content-Transfer-Encoding: binary');
98 header('Content-Length: '.strlen($data));
Eric Roberts0e4d2b62012-01-23 18:19:20 -060099
Andrey Andreevb38c5dd2012-02-29 14:07:45 +0200100 // Internet Explorer-specific headers
101 if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 header('Pragma: public');
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 }
Andrey Andreevb38c5dd2012-02-29 14:07:45 +0200106 else
107 {
108 header('Pragma: no-cache');
109 }
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 exit($data);
112 }
113}
114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115/* End of file download_helper.php */
Andrey Andreevb38c5dd2012-02-29 14:07:45 +0200116/* Location: ./system/helpers/download_helper.php */