I'm working on a way to serve up MP3 files through PHP and after some help form the SO massive, I got it working here
However, that example doesn't appear to work when I use it as the source in an audio tag like this
<html>
<head>
<title>Audio Tag Experiment</title>
</head>
<body>
<audio id='audio-element' src="music/mp3.php" autoplay controls>
Your browser does not support the audio element.
</audio>
</body>
</html>
and here's the PHP
<?php
$track = "lilly.mp3";
if(file_exists($track))
{
header("Content-Transfer-Encoding: binary");
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header('Content-length: ' . filesize($track));
header('Content-Disposition: filename="lilly.mp3"');
header('X-Pad: avoid browser bug');
Header('Cache-Control: no-cache');
readfile($track);
}else{
echo "no file";
}
So I'm thinking (and this may be a really bad idea, you tell me) that I might be able to set up Apache to serve a PHP file when someone requests an .MP3.
So I've three questions
An easy way to embed audio on a website is by using a sound hosting site, such as SoundCloud or Mixcloud. All you need to do is upload the file and receive an HTML embed code. Then copy and paste the embed code into the web page's code or WYSIWYG site editor. This works for most CMS platforms and website builders.
A developer will need to create a folder named upload in the Apache server's htdocs directory to support the PHP file upload component. This folder is where the HTML5 file uploads will be saved.
PHP and MySQL both are compatible with an Apache server. These two are open source and easy to set up. PHP runs on many platforms like Windows, Linux, and Unix. Because of these advantages, PHP is used on Apache servers.
There are some errors in your code:
audio/mpeg
.inline
.The rest looks fine. But I would also send the 404 status code if the file cannot be found.
$track = "lilly.mp3";
if (file_exists($track)) {
header("Content-Type: audio/mpeg");
header('Content-Length: ' . filesize($track));
header('Content-Disposition: inline; filename="lilly.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
readfile($track);
exit;
} else {
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
echo "no file";
}
You could simply make it so that you have a mod_rewrite rule to run every request for music/*.mp3 through your mp3.php file.
For example, something like this
RewriteEngine on
RewriteRule ^/music/(.*\.mp3) /music/mp3.php?file=$1 [L]
mp3.php can then pick up the requested file from $_GET['file'], but if you go with this approach I recommend you sanity check the filename, to ensure it only references a file in the desired directory.
//ensure filename just uses alphanumerics and underscore
if (preg_match('/^[a-z0-9_]+\.mp3$/i', $_GET['file']))
{
//check file exists and serve it
$track=$_SERVER['DOCUMENT_ROOT'].'/music/'.$_GET['file'];
if(file_exists($track))
{
header("Content-Type: audio/mpeg");
header('Content-length: ' . filesize($track));
//insert any other required headers...
//send data
readfile($track);
}
else
{
//filename OK, but just not here
header("HTTP/1.0 404 Not Found");
}
}
else
{
//bad request
header("HTTP/1.0 400 Forbidden");
}
Use the header x-sendfile instead of readfile for better performanse.
http://john.guen.in/past/2007/4/17/send_files_faster_with_xsendfile/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With