readfile.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. require('inc/config.inc.php');
  3. require('inc/model.php');
  4. /**
  5. * Description of VideoStream
  6. *
  7. * @author Rana
  8. * @link http://codesamplez.com/programming/php-html5-video-streaming-tutorial
  9. */
  10. class VideoStream
  11. {
  12. private $path = "";
  13. private $stream = "";
  14. private $buffer = 102400;
  15. private $start = -1;
  16. private $end = -1;
  17. private $size = 0;
  18. function __construct($filePath)
  19. {
  20. $this->path = $filePath;
  21. }
  22. /**
  23. * Open stream
  24. */
  25. private function open()
  26. {
  27. if (!($this->stream = fopen($this->path, 'rb'))) {
  28. die('Could not open stream for reading');
  29. }
  30. }
  31. /**
  32. * Set proper header to serve the video content
  33. */
  34. private function setHeader()
  35. {
  36. ob_get_clean();
  37. $splitted = explode('/', $this->path);
  38. header("Content-Type: application/x-download");
  39. header('Content-Disposition: attachment; filename="' . $splitted[sizeof($splitted) - 1] . '"');
  40. header("Cache-Control: max-age=2592000, public");
  41. header("Expires: ".gmdate('D, d M Y H:i:s', time()+2592000) . ' GMT');
  42. header("Last-Modified: ".gmdate('D, d M Y H:i:s', @filemtime($this->path)) . ' GMT' );
  43. $this->start = 0;
  44. $this->size = filesize($this->path);
  45. $this->end = $this->size - 1;
  46. //header("Accept-Ranges: 0-".$this->end);
  47. header('Accept-Ranges: bytes');
  48. if (isset($_SERVER['HTTP_RANGE'])) {
  49. $c_start = $this->start;
  50. $c_end = $this->end;
  51. list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
  52. if (strpos($range, ',') !== false) {
  53. header('HTTP/1.1 416 Requested Range Not Satisfiable');
  54. header("Content-Range: bytes $this->start-$this->end/$this->size");
  55. exit;
  56. }
  57. if ($range == '-') {
  58. $c_start = $this->size - substr($range, 1);
  59. }else{
  60. $range = explode('-', $range);
  61. $c_start = $range[0];
  62. $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $c_end;
  63. }
  64. $c_end = ($c_end > $this->end) ? $this->end : $c_end;
  65. if ($c_start > $c_end || $c_start > $this->size - 1 || $c_end >= $this->size) {
  66. header('HTTP/1.1 416 Requested Range Not Satisfiable');
  67. header("Content-Range: bytes $this->start-$this->end/$this->size");
  68. exit;
  69. }
  70. $this->start = $c_start;
  71. $this->end = $c_end;
  72. $length = $this->end - $this->start + 1;
  73. fseek($this->stream, $this->start);
  74. header('HTTP/1.1 206 Partial Content');
  75. header("Content-Length: ".$length);
  76. header("Content-Range: bytes $this->start-$this->end/".$this->size);
  77. }
  78. else
  79. {
  80. header("Content-Length: ".$this->size);
  81. }
  82. }
  83. /**
  84. * close curretly opened stream
  85. */
  86. private function end()
  87. {
  88. fclose($this->stream);
  89. exit;
  90. }
  91. /**
  92. * perform the streaming of calculated range
  93. */
  94. private function stream()
  95. {
  96. $i = $this->start;
  97. set_time_limit(0);
  98. while(!feof($this->stream) && $i <= $this->end) {
  99. $bytesToRead = $this->buffer;
  100. if(($i+$bytesToRead) > $this->end) {
  101. $bytesToRead = $this->end - $i + 1;
  102. }
  103. $data = fread($this->stream, $bytesToRead);
  104. echo $data;
  105. flush();
  106. $i += $bytesToRead;
  107. }
  108. }
  109. /**
  110. * Start streaming video content
  111. */
  112. function start()
  113. {
  114. $this->open();
  115. $this->setHeader();
  116. $this->stream();
  117. $this->end();
  118. }
  119. }
  120. $stream = new VideoStream(Model::decryptText(base64_decode(urldecode($_GET['file']))));
  121. $stream->start();