Использование php для вывода видео в формате mp4 - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.

Name: Использование php для вывода видео в формате mp4 fullscreencopydownloadembedprint


Your result can be seen below.

Result of php executing





Full code of Использование php для вывода видео в формате mp4.php

  1. <?php
  2. $video = 'http://sokolov-denis.com/play/DeeAFilm.mp4';
  3.  
  4. // Запрет прямого скачивания
  5. preg_match('/.*\//', $_SERVER['SCRIPT_URI'], $matches);
  6. $directOnly = isset($matches[0]) && isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], $matches[0]) !== false;
  7. $directOnly = $directOnly && isset($_SERVER['HTTP_RANGE']);
  8. // [HTTP_RANGE] => bytes=756153-
  9.  
  10. $file = $_SERVER['DOCUMENT_ROOT'] . $video;
  11. if (!is_readable($file) || !$directOnly) die('Облом!');
  12.  
  13. $fp = @fopen($file, 'rb');
  14.  
  15. $size   = filesize($file); // File size
  16. $length = $size;           // Content length
  17. $start  = 0;               // Start byte
  18. $end    = $size - 1;       // End byte
  19.  
  20. header('Content-type: video/mp4');
  21. header("Accept-Ranges: 0-$length");
  22. if (isset($_SERVER['HTTP_RANGE'])) {
  23.  
  24.     $c_start = $start;
  25.     $c_end   = $end;
  26.  
  27.     list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
  28.     if (strpos($range, ',') !== false) {
  29.         header('HTTP/1.1 416 Requested Range Not Satisfiable');
  30.         header("Content-Range: bytes $start-$end/$size");
  31.         exit;
  32.     }
  33.     if ($range == '-') {
  34.         $c_start = $size - substr($range, 1);
  35.     }else{
  36.         $range  = explode('-', $range);
  37.         $c_start = $range[0];
  38.         $c_end   = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
  39.     }
  40.     $c_end = ($c_end > $end) ? $end : $c_end;
  41.     if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
  42.         header('HTTP/1.1 416 Requested Range Not Satisfiable');
  43.         header("Content-Range: bytes $start-$end/$size");
  44.         exit;
  45.     }
  46.     $start  = $c_start;
  47.     $end    = $c_end;
  48.     $length = $end - $start + 1;
  49.     fseek($fp, $start);
  50.     header('HTTP/1.1 206 Partial Content');
  51. }
  52. header("Content-Range: bytes $start-$end/$size");
  53. header("Content-Length: ".$length);
  54.  
  55.  
  56. $buffer = 1024 * 8;
  57. while(!feof($fp) && ($p = ftell($fp)) <= $end) {
  58.  
  59.     if ($p + $buffer > $end) {
  60.         $buffer = $end - $p + 1;
  61.     }
  62.     set_time_limit(0);
  63.     echo fread($fp, $buffer);
  64.     flush();
  65. }
  66.  
  67. fclose($fp);
  68. exit();
  69. ?>
File Description
  • Использование php для вывода видео в формате mp4
  • PHP Code
  • 17 Jan-2021
  • 2.01 Kb
You can Share it: