I need to display an mp3 file on a php page. The mp3 file is 4MB. I am storing the mp3 outside the document root. The client does not want me to use htaccess, instead they want me to use a password only with the text box for the password imbedded in the page. I don't want to people to be able to find the mp3 file on the server and send a link to their friends. They have to be able to login to hear the mp3.
I need to call the php script, query the database for the name, path and file type and then in an imbedded source tag in the php generated html page call a script called movie.php?fileID=1.
I have already written the script. I checked and the info is being queried from the database. When I go to the script using the url domain.com/movie.php?fileID=1 I see the quicktime Q in the browser and it acts like it is downloadign the mp3 into the browser. But after a minute or so the Q turns into a broken Q and the mp3 does not play.
I also tried putting an embedded source tag in a file named test.htm and the player does not come up.
I tried using readfile instead of using fopen and fread. I have also tried using fopen and fread.
If anyone cah help please do.
<?php
ob_start();
session_start();
// Protect against reaching max execution time
set_time_limit(0);
// Force script termination if user aborts
ignore_user_abort(false);
include '/usr/local/apache/dbinclude/db.inc';
include 'include/error.inc';
include 'include/include.inc';
include 'include/generalQuery.inc';
//include '../include/databaseUpdates.inc';
set_error_handler("errorHandler");
//if (isset($_SESSION['login'])){
// Open a connection to the DBMS
if (!($connection = @ mysql_pconnect($hostName,
$username,
$password)))
showerror();
if (!mysql_select_db($databaseName, $connection))
showerror();
// query the download table
generalQuery($connection, "downloads");
// get the results of the query from the session and unset the session
$result = $_SESSION['result'];
unset($_SESSION['result']);
// write and close the session
session_write_close();
// fetch the row
$row = @ mysql_fetch_assoc($result);
$fileDir = $row['file_path']; // supply a path name.
$fileName = $row['file_name']; // supply a file name.
$fileString = $fileDir . '/' . $fileName; // combine the path and file
// translate file name properly for Internet Explorer.
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")){
$fileString = preg_replace('/\./', '%2e', $fileString, substr_count($fileString, '.') - 1);
}
// make sure the file exists before sending headers
if(!$fdl = @fopen($fileString,'rb')){
die("Cannot Open File!");
}
//else {
header("Content-type: audio/mpeg");
// header('Content-Type: application/mp3');
//readfile($fileString);
while(!feof($fdl)) {
echo fread($fdl, 8192);
// print(fread($fdl, 8192));
flush();
ob_flush();
}
fclose($fd1);
//}
// increment the download counter
//databaseUpdates(&$connection, "downloads");
//}
ob_end_flush();
?>