download_helper.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. * CodeIgniter Download Helpers
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Helpers
  21. * @category Helpers
  22. * @author ExpressionEngine Dev Team
  23. * @link http://codeigniter.com/user_guide/helpers/download_helper.html
  24. */
  25. // ------------------------------------------------------------------------
  26. /**
  27. * Force Download
  28. *
  29. * Generates headers that force a download to happen
  30. *
  31. * @access public
  32. * @param string filename
  33. * @param mixed the data to be downloaded
  34. * @return void
  35. */
  36. if ( ! function_exists('force_download'))
  37. {
  38. function force_download($filename = '', $data = '')
  39. {
  40. if ($filename == '' OR $data == '')
  41. {
  42. return FALSE;
  43. }
  44. // Try to determine if the filename includes a file extension.
  45. // We need it in order to set the MIME type
  46. if (FALSE === strpos($filename, '.'))
  47. {
  48. return FALSE;
  49. }
  50. // Grab the file extension
  51. $x = explode('.', $filename);
  52. $extension = end($x);
  53. // Load the mime types
  54. if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
  55. {
  56. include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
  57. }
  58. elseif (is_file(APPPATH.'config/mimes.php'))
  59. {
  60. include(APPPATH.'config/mimes.php');
  61. }
  62. // Set a default mime if we can't find it
  63. if ( ! isset($mimes[$extension]))
  64. {
  65. $mime = 'application/octet-stream';
  66. }
  67. else
  68. {
  69. $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
  70. }
  71. // Generate the server headers
  72. if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)
  73. {
  74. header('Content-Type: "'.$mime.'"');
  75. header('Content-Disposition: attachment; filename="'.$filename.'"');
  76. header('Expires: 0');
  77. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  78. header("Content-Transfer-Encoding: binary");
  79. header('Pragma: public');
  80. header("Content-Length: ".strlen($data));
  81. }
  82. else
  83. {
  84. header('Content-Type: "'.$mime.'"');
  85. header('Content-Disposition: attachment; filename="'.$filename.'"');
  86. header("Content-Transfer-Encoding: binary");
  87. header('Expires: 0');
  88. header('Pragma: no-cache');
  89. header("Content-Length: ".strlen($data));
  90. }
  91. exit($data);
  92. }
  93. }
  94. /* End of file download_helper.php */
  95. /* Location: ./system/helpers/download_helper.php */