"Downsides" depends on what you're trying to accomplish.
By setting the Location
header, you're sending a redirect. The browser, indeed, will receive a response with HTTP status code 302 ("Found") and the new URL of the resource. Basically, you're redirecting them to another resource.
In order for this to work, the resource you're redirecting them to must be publicly accessible. That is: it needs to be an existing document inside the "document root" of your server.
This approach has certain characteristics that, depending on your requirements, may be considered as downsides:
- As written above, the resource must be publicly accessible. So, you cannot have your MP3 files in a separate, non accessible folder. Indeed, suppose your document root is in
/var/www/public
, you may want to put these files in a non accessible folder, like /var/www/resources
, and serve them only through your PHP scripts.
- Your users will receive the URL of the resource, and so they can access the resource directly, without passing through your PHP script.
If these "characteristics" are not good for you, then you may want to avoid using the Location
header.
Some PHP code to serve MP3 files can be:
$file = 'local/path/to/your_audio_file.mp3'; // the path on the local file system - ie. don't use "http://www.example.com"!
header('Content-Description: File Transfer');
header('Content-Type: audio/mpeg'); // audio/mpeg is the correct mime type for mp3 files
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit; // Could be a good idea to exit at the end
Some of the headers may be redundant, but they're to assure the best cross-browser compatibility. I took them (and then adapted) from http://php.net/manual/en/function.readfile.php