download_helper.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2006, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * CodeIgniter Download Helpers
00020  *
00021  * @package             CodeIgniter
00022  * @subpackage  Helpers
00023  * @category    Helpers
00024  * @author              ExpressionEngine Dev Team
00025  * @link                http://codeigniter.com/user_guide/helpers/download_helper.html
00026  */
00027 
00028 // ------------------------------------------------------------------------
00029 
00030 /**
00031  * Force Download
00032  *
00033  * Generates headers that force a download to happen
00034  *
00035  * @access      public
00036  * @param       string  filename
00037  * @param       mixed   the data to be downloaded
00038  * @return      void
00039  */     
00040 if ( ! function_exists('force_download'))
00041 {
00042         function force_download($filename = '', $data = '')
00043         {
00044                 if ($filename == '' OR $data == '')
00045                 {
00046                         return FALSE;
00047                 }
00048 
00049                 // Try to determine if the filename includes a file extension.
00050                 // We need it in order to set the MIME type
00051                 if (FALSE === strpos($filename, '.'))
00052                 {
00053                         return FALSE;
00054                 }
00055         
00056                 // Grab the file extension
00057                 $x = explode('.', $filename);
00058                 $extension = end($x);
00059 
00060                 // Load the mime types
00061                 @include(APPPATH.'config/mimes'.EXT);
00062         
00063                 // Set a default mime if we can't find it
00064                 if ( ! isset($mimes[$extension]))
00065                 {
00066                         $mime = 'application/octet-stream';
00067                 }
00068                 else
00069                 {
00070                         $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
00071                 }
00072         
00073                 // Generate the server headers
00074                 if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
00075                 {
00076                         header('Content-Type: "'.$mime.'"');
00077                         header('Content-Disposition: attachment; filename="'.$filename.'"');
00078                         header('Expires: 0');
00079                         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
00080                         header("Content-Transfer-Encoding: binary");
00081                         header('Pragma: public');
00082                         header("Content-Length: ".strlen($data));
00083                 }
00084                 else
00085                 {
00086                         header('Content-Type: "'.$mime.'"');
00087                         header('Content-Disposition: attachment; filename="'.$filename.'"');
00088                         header("Content-Transfer-Encoding: binary");
00089                         header('Expires: 0');
00090                         header('Pragma: no-cache');
00091                         header("Content-Length: ".strlen($data));
00092                 }
00093         
00094                 exit($data);
00095         }
00096 }
00097 
00098 
00099 /* End of file download_helper.php */
00100 /* Location: ./system/helpers/download_helper.php */