Make Mp3 Seekable PHP

how do you support seeking of an mp3 when returning from a php script?

To support seeking, you often will have to support a range request.

From the RFC: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35

See also: Resumable downloads when using PHP to send the file?

PHP Streaming MP3

Here's what did the trick.

$dir = dirname($_SERVER['DOCUMENT_ROOT'])."/protected_content";
$filename = $_GET['file'];
$file = $dir."/".$filename;

$extension = "mp3";
$mime_type = "audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3";

if(file_exists($file)){
header('Content-type: {$mime_type}');
header('Content-length: ' . filesize($file));
header('Content-Disposition: filename="' . $filename);
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
readfile($file);
}else{
header("HTTP/1.0 404 Not Found");
}

Why is my mp3/ogg unseekable when served from Drupal 7 private files when it is seekable from public files

Drupal private files, like most authenticated PHP download scripts, route the file request through a PHP script. These scripts (almost) never support the appropriate headers, such as the Range header, required to do things like resume download or requests parts of the file as seeking does.

Usually the best solution to these problems is to not use a PHP script to output the file, as it add's unnecessary load to the server. Alternative solutions for authenticated downloads include X-Sendfile (if available or it can be installed), a temporary randomly-named symbolic link in a public directory, etc.



Related Topics



Leave a reply



Submit