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