PHP Audio Download Script

Any tutorials (or useful resources) should go in here.
Post Reply
wrichards8
Posts: 66
Joined: Thu Jan 12, 2012 3:54 pm
Contact:

PHP Audio Download Script

Post by wrichards8 »

This will output a listing of your music and let users download it and is a variation on my file listing script. This project requires the getID3() class so first go ahead and download that. You want to require or include the getid3.php file so you can take advantage of the getID3 class. You will then need to create a folder which you will store the audio files in so go create that. I recommend using a table for displaying the title, artist, album, year and genre.

[syntax=php]$filename = dirname(__FILE__);
/* Get the current path location */

$musicfolder = scandir($filename. "/audios");
/* Scan the audio folder and put make the array */

unset($musicfolder[0], $musicfolder[1]);
/* Unsets the first two elements */

$getID3 = new getID3;
/* Initialize the getID3 class */
echo "<table>\n";
echo "<thead>\n";
echo "<tr>\n";
echo "<th></th>\n";
echo "<th>Title</th>\n";
echo "<th>Artist</th>\n";
echo "<th>Album</th>\n";
echo "</tr>\n";
echo "</thead>\n";
echo "<tbody>\n";
/* Echo the header layout for the table with a new line at the end so table looks nice */

foreach($musicfolder as $music)
{
$tags = $getID3->analyze($filename. "/audios/" .$music);
/* Analyze the audio file to fetch tags*/

$download = substr(urlencode($music), 0, -4);
/* Urlencodes the song title so it can be passed to another page and removes the mp3 from the end */

echo "<tr>\n";
echo "<td><a href=\"audio.php?s={$download}\">Download</a></td>\n";
echo "<td>{$tags['tags']['id3v2']['title'][0]}</td>\n";
echo "<td>{$tags['tags']['id3v2']['artist'][0]}</td>\n";
echo "<td>{$tags['tags']['id3v2']['album'][0]}</td>\n";
echo "</tr>\n";
}
/* Output the loop to display the ID3 tags */

echo "</tbody>\n";
echo "</table>\n";
/* End of the table */

<?php $song = trim(urldecode($_GET["s"]));
/*URL decode and trim the users' input */

$filename = dirname(__FILE__);
/* Sets the current path to a folder */

$folder = $filename. "/audios";
/* Sets the current path to a folder */

$musicfolder = scandir($folder);
/* Scans the folder so we can throw up an error to say the song isn't inside*/

$file = "{$song}.mp3";
/* Puts the .mp3 on the end of the filename */

unset($musicfolder[0], $musicfolder[1]);
/* Unsets the first two elements of the $musicfolder */
if(in_array($file, $musicfolder)==FALSE)
{
$song = ucwords(htmlentities($song));
die("<p>'{$song}' is not found!</p>");
/* If the file requested by the user is not in the folder we output an error and die */
}
else
{
header("Content-Type: audio/mpg");
include("{$folder}/{$file}");
/* We set the content type to audio and include the MP3 */
}
?>[/syntax]

The advantage of this method is that we can use a deny from all in a .htaccess file inside the audio folder so the user can't access this folder directly. You may also wish to use the HTML5 audio tag so this doesn't have to be restricted to downloads
Post Reply