Good Day Everyone,
I need to use a PHP script to force Download a some MP3 (I want to make sure that it doesn't start streaming when clicking on the MP3 link). Here is the script am using right now:
<?php
@ini_set('error_reporting', E_ALL & ~ E_NOTICE);
@ini_set('zlib.output_compression', 'Off');
if(!isset($_GET['id']) || empty($_GET['id']))
{
header("HTTP/1.0 400 Bad Request");
echo ("No ID Give");
exit;
}
$filelist = array(
'000001' => array(
'filename' => 'djmix.mp3',
'folder' => 'http://www.example.com/music/download/',
'serverfldr' => '/home/content/html/music/download/'),
'000002' => array(
'filename' => 'othermix.mp3',
'folder' => 'http://www.example.com/music/download/',
'serverfldr' => '/home/content/html/music/download/')
);
$id = $_GET['id'];
$file = $filelist[$id]['filename'];
$folder = $filelist[$id]['folder'];
$serverpath = $filelist[$id]['serverfldr'];
if (is_file($serverpath . $file)){
$filesize = filesize($serverpath . $file);
$file = @fopen($serverpath . $file, "rb");
if ($file){
header('Pragma: public');
header('Expires: -1');
header('Cache-Control: public, must-revalidate, post-check=0, pre-check=0');
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Content-Type: application/octet-stream');
header('Content-Length:' . $filesize);
set_time_limit(0);
while(!feof($file))
{
print(@fread($file, 1024*8));
ob_flush();
flush();
if (connection_status()!=0)
{
@fclose($file);
exit;
}
}
// file save was a success
@fclose($file);
exit;
}
else{
echo ('File cannot be opened');
}
}
else {
echo('Cannot find a file');
};
exit;
?>
My biggest problem is that the files are quite big (from 50MB to 100MB), but it seems that on slow connection, there is some kind of timeout... Or Something happening because the download actually fails. (It really writes "FAILED" in Firefox).
On Firefox on my slow internet it gives me "FAILS" If I use a download manager (Speed Download for Mac) the download actually RESETS... Yes yes you read properly, it resets... What I mean by that? The download goes for a while and after a certain time it restarts from Zero.