Forum Moderators: coopster

Message Too Old, No Replies

Cannot play mp3 in the browser outputted by PHP

This is a problem in PHP pertaining outputting mp3

         

rockerzz501

8:21 am on Dec 29, 2008 (gmt 0)

10+ Year Member



Hi, I am new to webmaster world and a newbie. I have some problems with my PHP script not being able to stream mp3 in the browser (both IE and firefox). I have spent 2 days figuring out a solution and I still with no success.

Details:
PHP version:4.3.11
fopen: allowed (on)or enabled
server: apache ,Server API CGI/FastCGI

Overall process of the script:
1. flash player activate php script player file
2. player.php fetch the ID in the database and grab the mp3 filename for example: test.mp3
3. the filename is then processed by fopen
4. fopen then handles it to fread to output
5. PHP headers is then set to audio/mp3 to play
6. done

php player script:


<?php
//set headers to mp3
header("Content-type: audio/mpeg");
header('Content-Length: '.filesize($file));
//screen input against malicious entry
if (! preg_match('/^[-a-z.-@,\'\s]*$/i',$_GET['file']))
{
die('Invalid name proved, the name may only contain a-z, A-Z, 0-9, "-", "_" and spaces.');
}
else
$empty=strlen($_GET['file']);
if ($empty==0)
{
die('The text field cannot be empty');
}
else
{
//the input data is clean, retrieve text data input
$ID = trim($_GET['file']);

}



//Connect to MySQL database after sanitizing the data
$username = "------------";
$password = "------------";
$hostname = "-------------";
$database = "--------------";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
//select a database to work with
$selected = mysql_select_db($database,$dbhandle)
or die("Could not select $database");
//Escape variables for use in MySQL
$ID = mysql_real_escape_string($ID);
// sending query
$result = mysql_query("SELECT `filename` FROM `table` WHERE `ID`='$ID'")
or die(mysql_error());


$row = mysql_fetch_array( $result )
or die("Invalid query: " . mysql_error());
$filename = $row['filename'];
$file ='/home/content/--------path'.filename;
$fh= fopen($file,"rb") or die('Could not open file');
while (!feof($fh)) {
echo fread($fh,8192);
}
fclose($fh);
mysql_close($dbhandle);
?>

TESTING DONE:
1. I test the script in localhost and it works.
2. I then upload the script to the server but it is not playing, instead it output something nonsense to the browser (see below):

ÿú’`š

3. I tried all possibilities of using correct headers but it still wont play.
4. I even set in the htaccess to addtype to recognize mp3 but again failed to play.

What is strange is that I modified the script to output jpeg file but it wont show the image and still gibberish output. So it is not an mp3 issue only. It seems browser cannot understand what is being outputted despite correct headers are set.

For example if a url: www.somesite.com/mp3player.php?file=7868878

corresponds to play TEST.mp3

but when I type the url to the browser address bar to test, it instead outputted:

ÿú’`š

I check fopen and it is allowed. My intention of this script is to hide the real location of the mp3 file in the server by showing URL like this: www.somesite.com/mp3player.php?file=7868878 instead of actual location.

I need advice and comments from PHP gurus here in order to solve this issue. Thank you so much and Happy Holidays.

Cheers,
rockerzz501

rockerzz501

3:07 am on Dec 30, 2008 (gmt 0)

10+ Year Member



Hi to all, any ideas? As an update, I even try to execute another PHP script to output mp3 from PHP manual:http://www.php.net/fread


<?php
@ob_end_clean();
// Only allow mp3 files
$allowedFileType = "mp3";
// Set the filename based on the URL's query string
$theFile = "test.mp3";
// Get info about the file
$f = pathinfo($theFile);
// Check the extension against allowed file types
if(strtolower($f['extension']) != strtolower($allowedFileType)) exit;
// Make sure the file exists
if (!file_exists($theFile)) exit;
// Set headers
header("Pragma: public");
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: private");
header("Content-Transfer-Encoding: binary");
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
// This line causes the browser's "save as" dialog
header( 'Content-Disposition: attachment; filename="'.$f['basename'].'"' );
// Length required for Internet Explorer
header("Content-Length: ".@urldecode(@filesize($theFile)));
// Open file
if (($f = fopen($theFile, 'rb')) === false) exit;
// Push file
while (!feof($f)) {
echo fread($f, (1*(1024*1024)));
flush();
@ob_flush();
}
// Close file
fclose($f);
exit;
?>

And this is the output: ÿú’`Iä

The site is hosted in Godaddy economy hosting (free hosting after registering their domains under linux plan). Can anyone help please? Thanks in advance.

eelixduppy

3:51 am on Dec 30, 2008 (gmt 0)



Can't see anything wrong here. Could it be possible there is corrupted data somewhere? I think even then, though, the browser would say so. If I had to guess I'd say it might have something to do with the server configuration and not the script itself (as you might have guessed, as well). Check the headers of the server response and see if they are correct as an additional check to be sure. If that doesn't show something, you might have to contact GoDaddy support on this one.

rockerzz501

9:53 am on Dec 30, 2008 (gmt 0)

10+ Year Member



Thanks for the tip. I will figure this out soon. I think this has something to do also with my free hosting shared account maybe i have limited rights. I will contact the hosting agency if this cannot be resolved at my level.

eelixduppy

5:53 pm on Dec 30, 2008 (gmt 0)



Check to see if you are getting any errors from the header() call. Possibly they aren't being sent out at all.

jojy

11:30 pm on Dec 31, 2008 (gmt 0)

10+ Year Member



delete following lines
$fh= fopen($file,"rb") or die('Could not open file');
while (!feof($fh)) {
echo fread($fh,8192);
}

and replace with these one
readfile($file);
die();

and correct header for mp3 is audio/mp3

Good luck