/halogy/helpers/download_helper.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 100 lines · 46 code · 12 blank · 42 comment · 8 complexity · c36fce64c9f41bc7f78766b3426c544f MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2009, 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. @include(APPPATH.'config/mimes'.EXT);
  55. // Set a default mime if we can't find it
  56. if ( ! isset($mimes[$extension]))
  57. {
  58. $mime = 'application/octet-stream';
  59. }
  60. else
  61. {
  62. $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
  63. }
  64. // Generate the server headers
  65. if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
  66. {
  67. header('Content-Type: "'.$mime.'"');
  68. header('Content-Disposition: attachment; filename="'.$filename.'"');
  69. header('Expires: 0');
  70. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  71. header("Content-Transfer-Encoding: binary");
  72. header('Pragma: public');
  73. header("Content-Length: ".strlen($data));
  74. }
  75. else
  76. {
  77. header('Content-Type: "'.$mime.'"');
  78. header('Content-Disposition: attachment; filename="'.$filename.'"');
  79. header("Content-Transfer-Encoding: binary");
  80. header('Expires: 0');
  81. header('Pragma: no-cache');
  82. header("Content-Length: ".strlen($data));
  83. }
  84. exit($data);
  85. }
  86. }
  87. /* End of file download_helper.php */
  88. /* Location: ./system/helpers/download_helper.php */