Created
June 25, 2016 22:15
-
-
Save techjewel/352ca172380b457e005e5718a1eab5f5 to your computer and use it in GitHub Desktop.
Stream a mp3 file using php when you need to protect the file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function zcast_stream($file, $content_type = 'application/octet-stream') { | |
@error_reporting(0); | |
// Make sure the files exists, otherwise we are wasting our time | |
if (!file_exists($file)) { | |
header("HTTP/1.1 404 Not Found"); | |
exit; | |
} | |
// Get file size | |
$filesize = sprintf("%u", filesize($file)); | |
// Handle 'Range' header | |
if(isset($_SERVER['HTTP_RANGE'])){ | |
$range = $_SERVER['HTTP_RANGE']; | |
}elseif($apache = apache_request_headers()){ | |
$headers = array(); | |
foreach ($apache as $header => $val){ | |
$headers[strtolower($header)] = $val; | |
} | |
if(isset($headers['range'])){ | |
$range = $headers['range']; | |
} | |
else $range = FALSE; | |
} else $range = FALSE; | |
//Is range | |
if($range){ | |
$partial = true; | |
list($param, $range) = explode('=',$range); | |
// Bad request - range unit is not 'bytes' | |
if(strtolower(trim($param)) != 'bytes'){ | |
header("HTTP/1.1 400 Invalid Request"); | |
exit; | |
} | |
// Get range values | |
$range = explode(',',$range); | |
$range = explode('-',$range[0]); | |
// Deal with range values | |
if ($range[0] === ''){ | |
$end = $filesize - 1; | |
$start = $end - intval($range[0]); | |
} else if ($range[1] === '') { | |
$start = intval($range[0]); | |
$end = $filesize - 1; | |
}else{ | |
// Both numbers present, return specific range | |
$start = intval($range[0]); | |
$end = intval($range[1]); | |
if ($end >= $filesize || (!$start && (!$end || $end == ($filesize - 1)))) $partial = false; // Invalid range/whole file specified, return whole file | |
} | |
$length = $end - $start + 1; | |
} | |
// No range requested | |
else $partial = false; | |
// Send standard headers | |
header("Content-Type: $content_type"); | |
header("Content-Length: $length"); | |
header('Accept-Ranges: bytes'); | |
header('Connection: keep-alive'); | |
// send extra headers for range handling... | |
if ($partial) { | |
header('HTTP/1.1 206 Partial Content'); | |
header("Content-Range: bytes $start-$end/$filesize"); | |
if (!$fp = fopen($file, 'rb')) { | |
header("HTTP/1.1 500 Internal Server Error"); | |
exit; | |
} | |
if ($start) fseek($fp,$start); | |
print(fread($fp,$length)); | |
fclose($fp); | |
} | |
//just send the whole file | |
else readfile($file); | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment