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
Avoiding Form Resubmit in PHP When Pressing F5
How to Return Back Twice in Laravel
Is MySQL_Insert_Id Safe to Use
Fastest Way to Add Prefix to Array Keys
Why Do Changes to Some PHP Files Take So Long to Show on the Live Site
How to Enable MySQLi Support for PHP's Cli
Mysqli Prepared Statements with in Operator
Password_Hash Returns Different Value Every Time
How to Redirect After Download in Laravel
Simple PHP Contact Form with Firebase Hosting
PHP Simplexml: Insert Node at Certain Position
Get All Instances of a Class in PHP
How to Pass Jquery Variables to PHP Variable
Redirect to Page and Send Custom Http Headers