Skip to content

Instantly share code, notes, and snippets.

@pawtwa
Forked from codler/gist:3906826
Last active May 23, 2021 15:55
Show Gist options
  • Save pawtwa/d16a3affd1d811819f95e63d358c8509 to your computer and use it in GitHub Desktop.
Save pawtwa/d16a3affd1d811819f95e63d358c8509 to your computer and use it in GitHub Desktop.
Support HTTP Header Range, mp4, php.php/mp4.mp4
<?php
# Nginx don't have PATH_INFO
if (!isset($_SERVER['PATH_INFO'])) {
$_SERVER['PATH_INFO'] = substr($_SERVER["ORIG_SCRIPT_FILENAME"], strlen($_SERVER["SCRIPT_FILENAME"]));
}
$request = substr($_SERVER['PATH_INFO'], 1);
$file = $request;
$fp = @fopen($file, 'rb');
$size = filesize($file); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
header('Content-type: video/mp4');
header("Accept-Ranges: 0-$length");
if (isset($_SERVER['HTTP_RANGE'])) {
$c_start = $start;
$c_end = $end;
list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
if (strpos($range, ',') !== false) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $start-$end/$size");
exit;
}
if ($range == '-') {
$c_start = $size - substr($range, 1);
}else{
$range = explode('-', $range);
$c_start = $range[0];
$c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
}
$c_end = ($c_end > $end) ? $end : $c_end;
if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $start-$end/$size");
exit;
}
$start = $c_start;
$end = $c_end;
$length = $end - $start + 1;
fseek($fp, $start);
header('HTTP/1.1 206 Partial Content');
}
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: ".$length);
$buffer = 1024 * 8;
while(!feof($fp) && ($p = ftell($fp)) <= $end) {
if ($p + $buffer > $end) {
$buffer = $end - $p + 1;
}
set_time_limit(0);
echo fread($fp, $buffer);
flush();
}
fclose($fp);
exit();
?>
<?php
public function downloadVideo(Request $request, Response $response, $args): Response
{
$originalFileName = $args['original_file_name'];
$media = $this->mediaFacade->byOriginalFileName($originalFileName);
if ($media instanceof ErrorDto) {
return RequestResponseService::send400(null, $media);
}
$mediaPath = $this->mediaFacade->getMediaPath($media->getAttribute('file_name'));
if ($mediaPath instanceof ErrorDto) {
return RequestResponseService::send400(null, $mediaPath);
}
if (!file_exists($mediaPath)) {
return RequestResponseService::send404();
}
$file = fopen($mediaPath, 'rb');
$stream = new Stream($file);
$size = filesize($mediaPath);
$length = $size;
$start = 0;
$end = $size - 1;
$response =
$response->withHeader('Content-Type', mime_content_type($mediaPath))->withHeader('Accept-Ranges', 'bytes');
if (isset($_SERVER['HTTP_RANGE'])) {
list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
if (str_contains($range, ',')) {
return $response->withStatus(416)->withHeader('Content-Range', "bytes $start-$end/$size");
}
$c_start = $start;
$c_end = $end;
if ($range == '-') {
$c_start = $size - intval(substr($range, 1));
} else {
$range = explode('-', $range);
$c_start = intval($range[0]);
$c_end = (isset($range[1]) && is_numeric($range[1])) ? intval($range[1]) : $size;
}
$c_end = ($c_end > $end) ? $end : $c_end;
if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
return $response->withStatus(416)->withHeader('Content-Range', "bytes $start-$end/$size");
}
$start = $c_start;
$end = $c_end;
$length = $end - $start + 1;
$stream->seek($start);
$response = $response->withStatus(206);
}
$response =
$response->withHeader('Content-Range', "bytes $start-$end/$size")->withHeader('Content-Length', $length);
$buffer = 1024 * 8;
$content = '';
while (!$stream->eof() && ($p = $stream->tell()) <= $end) {
if ($p + $buffer > $end) {
$buffer = $end - $p + 1;
}
$contentPart = $stream->read($buffer);
if ($contentPart) {
$content .= $contentPart;
}
}
$stream->close();
$response = $response->withHeader('Content-Disposition', 'attachment; filename="' . $originalFileName . '"')
->withHeader('Content-Description', 'File Transfer')
->withHeader('Content-Transfer-Encoding', 'binary')->withHeader('Expires', '0')
->withHeader('Cache-Control', 'must-revalidate')->withHeader('Pragma', 'public');
$response->getBody()->write($content);
return $response;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment