source: pro-violet-viettel/sourcecode/system/helpers/download_helper.php

Last change on this file was 289, checked in by dungnv, 11 years ago
File size: 2.7 KB
RevLine 
[289]1<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
7 * @package             CodeIgniter
8 * @author              ExpressionEngine Dev Team
9 * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @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
39 */
40if ( ! 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                }
55
56                // Grab the file extension
57                $x = explode('.', $filename);
58                $extension = end($x);
59
60                // Load the mime types
61                if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
62                {
63                        include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
64                }
65                elseif (is_file(APPPATH.'config/mimes.php'))
66                {
67                        include(APPPATH.'config/mimes.php');
68                }
69
70                // Set a default mime if we can't find it
71                if ( ! isset($mimes[$extension]))
72                {
73                        $mime = 'application/octet-stream';
74                }
75                else
76                {
77                        $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
78                }
79
80                // Generate the server headers
81                if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)
82                {
83                        header('Content-Type: "'.$mime.'"');
84                        header('Content-Disposition: attachment; filename="'.$filename.'"');
85                        header('Expires: 0');
86                        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
87                        header("Content-Transfer-Encoding: binary");
88                        header('Pragma: public');
89                        header("Content-Length: ".strlen($data));
90                }
91                else
92                {
93                        header('Content-Type: "'.$mime.'"');
94                        header('Content-Disposition: attachment; filename="'.$filename.'"');
95                        header("Content-Transfer-Encoding: binary");
96                        header('Expires: 0');
97                        header('Pragma: no-cache');
98                        header("Content-Length: ".strlen($data));
99                }
100
101                exit($data);
102        }
103}
104
105
106/* End of file download_helper.php */
107/* Location: ./system/helpers/download_helper.php */
Note: See TracBrowser for help on using the repository browser.