blob: 95c94a1b8f01192ad5be518dde7bae435125ef7f [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevb38c5dd2012-02-29 14:07:45 +02008 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * CodeIgniter Download Helpers
42 *
43 * @package CodeIgniter
44 * @subpackage Helpers
45 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000047 * @link http://codeigniter.com/user_guide/helpers/download_helper.html
48 */
49
50// ------------------------------------------------------------------------
51
Derek Allard2067d1a2008-11-13 22:59:24 +000052if ( ! function_exists('force_download'))
53{
Timothy Warren01b129a2012-04-27 11:36:50 -040054 /**
55 * Force Download
56 *
57 * Generates headers that force a download to happen
58 *
59 * @param string filename
60 * @param mixed the data to be downloaded
Alex Bilbief512b732012-06-16 11:15:19 +010061 * @param bool whether to try and send the actual file MIME type
Gabriel Potkánycea5fb72015-02-04 08:22:06 +010062 * @return void
Timothy Warren01b129a2012-04-27 11:36:50 -040063 */
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020064 function force_download($filename = '', $data = '', $set_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000065 {
Alex Bilbie773ccc32012-06-02 11:11:08 +010066 if ($filename === '' OR $data === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000067 {
Gabriel Potkánycea5fb72015-02-04 08:22:06 +010068 return;
Derek Allard2067d1a2008-11-13 22:59:24 +000069 }
Andrey Andreev53fff912012-11-22 16:57:23 +020070 elseif ($data === NULL)
71 {
Andrey Andreev06879112013-01-29 15:05:02 +020072 if (@is_file($filename) && ($filesize = @filesize($filename)) !== FALSE)
Andrey Andreev53fff912012-11-22 16:57:23 +020073 {
74 $filepath = $filename;
75 $filename = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $filename));
76 $filename = end($filename);
77 }
78 else
79 {
Gabriel Potkánycea5fb72015-02-04 08:22:06 +010080 return;
Andrey Andreev53fff912012-11-22 16:57:23 +020081 }
82 }
83 else
84 {
85 $filesize = strlen($data);
86 }
Derek Allard2067d1a2008-11-13 22:59:24 +000087
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020088 // Set the default MIME type to send
89 $mime = 'application/octet-stream';
90
Andrey Andreevfce2ed62012-03-11 22:04:48 +020091 $x = explode('.', $filename);
92 $extension = end($x);
93
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020094 if ($set_mime === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +000095 {
Andrey Andreevfce2ed62012-03-11 22:04:48 +020096 if (count($x) === 1 OR $extension === '')
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020097 {
Andrey Andreevfce2ed62012-03-11 22:04:48 +020098 /* If we're going to detect the MIME type,
99 * we'll need a file extension.
100 */
Gabriel Potkánycea5fb72015-02-04 08:22:06 +0100101 return;
Andrey Andreevb38c5dd2012-02-29 14:07:45 +0200102 }
103
Andrey Andreevb38c5dd2012-02-29 14:07:45 +0200104 // Load the mime types
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300105 $mimes =& get_mimes();
Andrey Andreevb38c5dd2012-02-29 14:07:45 +0200106
107 // Only change the default MIME if we can find one
108 if (isset($mimes[$extension]))
109 {
110 $mime = is_array($mimes[$extension]) ? $mimes[$extension][0] : $mimes[$extension];
111 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
Andrey Andreevfce2ed62012-03-11 22:04:48 +0200114 /* It was reported that browsers on Android 2.1 (and possibly older as well)
115 * need to have the filename extension upper-cased in order to be able to
116 * download it.
117 *
118 * Reference: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/
119 */
Andrey Andreev3d933b62012-03-11 22:08:57 +0200120 if (count($x) !== 1 && isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/Android\s(1|2\.[01])/', $_SERVER['HTTP_USER_AGENT']))
Andrey Andreevfce2ed62012-03-11 22:04:48 +0200121 {
122 $x[count($x) - 1] = strtoupper($extension);
123 $filename = implode('.', $x);
124 }
Andrey Andreevae31eb52012-05-17 14:54:15 +0300125
Andrey Andreev53fff912012-11-22 16:57:23 +0200126 if ($data === NULL && ($fp = @fopen($filepath, 'rb')) === FALSE)
127 {
Gabriel Potkánycea5fb72015-02-04 08:22:06 +0100128 return;
Andrey Andreev53fff912012-11-22 16:57:23 +0200129 }
130
Sam Libbd9dd32012-04-23 23:13:46 +0800131 // Clean output buffer
Andrey Andreev53fff912012-11-22 16:57:23 +0200132 if (ob_get_level() !== 0 && @ob_end_clean() === FALSE)
Michiel Vugteveen28ff2922012-08-14 10:55:46 +0200133 {
Andrey Andreev88d16d72014-01-28 12:28:42 +0200134 @ob_clean();
Michiel Vugteveen28ff2922012-08-14 10:55:46 +0200135 }
Andrey Andreevfce2ed62012-03-11 22:04:48 +0200136
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 // Generate the server headers
dododedodonl4da94782012-03-10 13:56:17 +0100138 header('Content-Type: '.$mime);
Eric Roberts0e4d2b62012-01-23 18:19:20 -0600139 header('Content-Disposition: attachment; filename="'.$filename.'"');
140 header('Expires: 0');
Andrey Andreevb38c5dd2012-02-29 14:07:45 +0200141 header('Content-Transfer-Encoding: binary');
Andrey Andreev53fff912012-11-22 16:57:23 +0200142 header('Content-Length: '.$filesize);
Eric Roberts0e4d2b62012-01-23 18:19:20 -0600143
Andrey Andreevb38c5dd2012-02-29 14:07:45 +0200144 // Internet Explorer-specific headers
145 if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
Andrey Andreevd87f6bf2012-10-24 22:12:53 +0300147 header('Cache-Control: no-cache, no-store, must-revalidate');
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
Andrey Andreevd87f6bf2012-10-24 22:12:53 +0300149
150 header('Pragma: no-cache');
Barry Mienydd671972010-10-04 16:33:58 +0200151
Andrey Andreev53fff912012-11-22 16:57:23 +0200152 // If we have raw data - just dump it
153 if ($data !== NULL)
154 {
155 exit($data);
156 }
157
158 // Flush 1MB chunks of data
159 while ( ! feof($fp) && ($data = fread($fp, 1048576)) !== FALSE)
160 {
161 echo $data;
162 }
163
164 fclose($fp);
165 exit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
167}