PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/server/system/helpers/download.php

https://github.com/ozten/mobhat
PHP | 105 lines | 83 code | 2 blank | 20 comment | 2 complexity | cc55dd29d3a0c7dd591a81967c8bed89 MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * Download helper class.
  4. *
  5. * $Id: download.php 3769 2008-12-15 00:48:56Z zombor $
  6. *
  7. * @package Core
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class download_Core {
  13. /**
  14. * Force a download of a file to the user's browser. This function is
  15. * binary-safe and will work with any MIME type that Kohana is aware of.
  16. *
  17. * @param string a file path or file name
  18. * @param mixed data to be sent if the filename does not exist
  19. * @param string suggested filename to display in the download
  20. * @return void
  21. */
  22. public static function force($filename = NULL, $data = NULL, $nicename = NULL)
  23. {
  24. if (empty($filename))
  25. return FALSE;
  26. if (is_file($filename))
  27. {
  28. // Get the real path
  29. $filepath = str_replace('\\', '/', realpath($filename));
  30. // Set filesize
  31. $filesize = filesize($filepath);
  32. // Get filename
  33. $filename = substr(strrchr('/'.$filepath, '/'), 1);
  34. // Get extension
  35. $extension = strtolower(substr(strrchr($filepath, '.'), 1));
  36. }
  37. else
  38. {
  39. // Get filesize
  40. $filesize = strlen($data);
  41. // Make sure the filename does not have directory info
  42. $filename = substr(strrchr('/'.$filename, '/'), 1);
  43. // Get extension
  44. $extension = strtolower(substr(strrchr($filename, '.'), 1));
  45. }
  46. // Get the mime type of the file
  47. $mime = Kohana::config('mimes.'.$extension);
  48. if (empty($mime))
  49. {
  50. // Set a default mime if none was found
  51. $mime = array('application/octet-stream');
  52. }
  53. // Generate the server headers
  54. header('Content-Type: '.$mime[0]);
  55. header('Content-Disposition: attachment; filename="'.(empty($nicename) ? $filename : $nicename).'"');
  56. header('Content-Transfer-Encoding: binary');
  57. header('Content-Length: '.sprintf('%d', $filesize));
  58. // More caching prevention
  59. header('Expires: 0');
  60. if (Kohana::user_agent('browser') === 'Internet Explorer')
  61. {
  62. // Send IE headers
  63. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  64. header('Pragma: public');
  65. }
  66. else
  67. {
  68. // Send normal headers
  69. header('Pragma: no-cache');
  70. }
  71. // Clear the output buffer
  72. Kohana::close_buffers(FALSE);
  73. if (isset($filepath))
  74. {
  75. // Open the file
  76. $handle = fopen($filepath, 'rb');
  77. // Send the file data
  78. fpassthru($handle);
  79. // Close the file
  80. fclose($handle);
  81. }
  82. else
  83. {
  84. // Send the file data
  85. echo $data;
  86. }
  87. }
  88. } // End download