Forum Moderators: coopster
header("Location: " .$link);
Every post I have ever read about using php for mp3 playback is complicated, but this simple one liner has always worked for me. Now I don't know what is going on.
I tried this code:
$mp3file = $_GET["filename"]; // e.g. /music/song.mp3 // send content-type and length headers
header("Content-Type: audio/mpeg");
header("Content-length: ".filesize($mp3file));
// open the file and spew it back to the client
$fp = fopen($mp3file, 'rb');
fpassthru($fp);
It just echoes the contents of the file to the browser window...
I am totally confused... What do I need to do to play mp3 files again?
thanks,
brian
<?
//open mp3 to browser
//03-17-2008$mp3file = $_GET["filename"]; // e.g. /music/song.mp3
if(!isset($_GET['filename']) ¦¦ empty($mp3file)){
die ("Unable to retrieve file.");
}
//echo "\$mp3file: $mp3file<br>\n";
// send content-type and length headers
header("Content-Type: audio/mpeg");
header("Content-length: ".filesize($mp3file));
header("Content-Disposition: attachment; filename=\"$mp3file\"");
//method 1
// open the file and spew it back to the client
$fp = fopen($mp3file, 'rb');
fpassthru($fp);
exit;
/*
//method 2
$file = @fopen($mp3file,"rb");
if ($file) {
while(!feof($file)) {
print(fread($file, 1024*8));
flush();
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}
*/
?>
if you want to integrate the song to play within the browser, consider using the HTML <embed> tags. This is the most simple way to have the song play without having to first download it (and then opening it).
another approach that I would consider if you were to integrate music on your site is something called the XSPF player. It looks beter and has the playlist capability. I've used this a little and its pretty neat. Its written in flash, and all you need to do is supply the URLs of your music files. I think it supports several formats. [musicplayer.sourceforge.net...]