Skip to content

Instantly share code, notes, and snippets.

@milankragujevic
Created July 1, 2021 13:19
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milankragujevic/b766018f322fd4214b2a2af3217963a4 to your computer and use it in GitHub Desktop.
Save milankragujevic/b766018f322fd4214b2a2af3217963a4 to your computer and use it in GitHub Desktop.
PHP streaming proxy with support for Range requests (perfect for proxying video files with progressive download MP4 streaming)
<?php
error_reporting(0);
set_time_limit(0);
ob_end_clean();
$url = $_GET['url'];
if(isset($_SERVER['HTTP_RANGE'])) {
stream_context_set_default([
'http' => [
'header' => "Range: " . $_SERVER['HTTP_RANGE']
]
]);
}
$headers = get_headers($url, 1);
header($headers[0]);
if(isset($headers['Content-Type'])) { header('Content-Type: ' . $headers['Content-Type']); }
if(isset($headers['Content-Length'])) { header('Content-Length: ' . $headers['Content-Length']); }
if(isset($headers['Accept-Ranges'])) { header('Accept-Ranges: ' . $headers['Accept-Ranges']); }
if(isset($headers['Content-Range'])) { header('Content-Range: ' . $headers['Content-Range']); }
if($_SERVER['REQUEST_METHOD'] == 'HEAD') { exit; }
$fp = fopen($url, 'rb');
while(!feof($fp)) {
echo fread($fp, 1024 * 256); flush();
}
fclose($fp);
@sasha172017
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment