Source for file download_functions.php

Documentation is available at download_functions.php

  1. <?php
  2. /**
  3.  * CodeIgniter
  4.  *
  5.  * An open source application development framework for PHP 4.3.2 or newer
  6.  *
  7.  * @package        CodeIgniter
  8.  * @subpackage     Helpers
  9.  * @author        ExpressionEngine Dev Team
  10.  * @copyright    Copyright (c) 2006, EllisLab, Inc.
  11.  * @license        http://codeigniter.com/user_guide/license.html
  12.  * @link        http://codeigniter.com
  13.  * @since        Version 1.0
  14.  * @filesource
  15.  */
  16.  
  17. // ------------------------------------------------------------------------
  18.  
  19. /**
  20.  * CodeIgniter Download Helpers
  21.  *
  22.  * @author        ExpressionEngine Dev Team
  23.  * @link        http://codeigniter.com/user_guide/helpers/download_helper.html
  24.  */
  25.  
  26. // ------------------------------------------------------------------------
  27.  
  28. if (function_exists('force_download')) {
  29.     /**
  30.      * Force Download
  31.      *
  32.      * Generates headers that force a download to happen
  33.      *
  34.      * @access    public
  35.      * @param    string    filename
  36.      * @param    mixed    the data to be downloaded
  37.      * @return    void 
  38.      */
  39.     function force_download($filename ''$data null)
  40.     {
  41.         if ($filename == '')
  42.         {
  43.             return FALSE;
  44.         }
  45.         
  46.         if ($data === null{
  47.             $datalen filesize($filename);
  48.         }
  49.         else {
  50.             $datalen strlen($data);
  51.         }
  52.  
  53.         // Try to determine if the filename includes a file extension.
  54.         // We need it in order to set the MIME type
  55.         if (FALSE === strpos($filename'.'))
  56.         {
  57.             return FALSE;
  58.         }
  59.     
  60.         // Grab the file extension
  61.         $x explode('.'$filename);
  62.         $extension end($x);
  63.  
  64.         // Load the mime types
  65.         @include(APP_SYSTEM_PATH.'/config/mimes.php');
  66.     
  67.         // Set a default mime if we can't find it
  68.         if isset($mimes[$extension]))
  69.         {
  70.             $mime 'application/octet-stream';
  71.         }
  72.         else
  73.         {
  74.             $mime (is_array($mimes[$extension])) $mimes[$extension][0$mimes[$extension];
  75.         }
  76.     
  77.         // Generate the server headers
  78.         if (strstr($_SERVER['HTTP_USER_AGENT']"MSIE"))
  79.         {
  80.             header('Content-Type: "'.$mime.'"');
  81.             header('Content-Disposition: attachment; filename="'.basename($filename).'"');
  82.             header('Expires: 0');
  83.             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  84.             header("Content-Transfer-Encoding: binary");
  85.             header('Pragma: public');
  86.             header("Content-Length: ".$datalen);
  87.         }
  88.         else
  89.         {
  90.             header('Content-Type: "'.$mime.'"');
  91.             header('Content-Disposition: attachment; filename="'.basename($filename).'"');
  92.             header("Content-Transfer-Encoding: binary");
  93.             header('Expires: 0');
  94.             header('Pragma: no-cache');
  95.             header("Content-Length: ".$datalen);
  96.         }
  97.     
  98.         readfile($filename);
  99.     }
  100. }
  101.  
  102. ?>

Documentation generated on Tue, 22 Nov 2011 13:28:55 -0200 by phpDocumentor 1.4.3