readfile.php 3.6 KB

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