Forum Moderators: coopster
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
<?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.