The code below comes from an excellent anti-leeching script-
Download Sentinel ++
It's designed to allow people to download files using temporary links. As it's written, it forces files to be downloaded, but I need it to
play files. (I'm hosting a bunch of wma files and I want to prevent bulk downloading.) I've modified the script so that the Content-Disposition is inline - and it works for images, and it works for wma files when using IE. But when using Firefox, it's still forcing downloading of wma files.
If anyone can spot anything that might be causing this, I'd be most grateful for the help!
You can see it in action at
http://stickpuppy.com/dsplus/mplus.php - click on the amazed.
png link to see it work, expand one of the year folders and click a wma file to see it fail (in FF).
- PHP: Select all
function DownLoadFile($fil,$p)
{
if (connection_status()!=0) return(FALSE);
$extension = strtolower(substr(strrchr($fn, "."), 1));
switch($extension){
case "asf": $type = "video/x-ms-asf"; break;
case "avi": $type = "video/x-msvideo"; break;
case "exe": $type = "application/octet-stream"; break;
case "mov": $type = "video/quicktime"; break;
case "mp3": $type = "audio/mpeg"; break;
case "mpg": $type = "video/mpeg"; break;
case "mpeg": $type = "video/mpeg"; break;
case "rar": $type = "encoding/x-compress"; break;
case "txt": $type = "text/plain"; break;
case "wav": $type = "audio/wav"; break;
case "wma": $type = "audio/x-ms-wma"; break;
case "wmv": $type = "video/x-ms-wmv"; break;
case "zip": $type = "application/x-zip-compressed"; break;
default: $type = "application/force-download"; break;
}
$fn = basename($fil);
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Content-Transfer-Encoding: binary\n");
header('Content-Type: $type');
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
//workaround for IE filename bug with multiple periods / multiple dots in filename
//that adds square brackets to filename - eg. setup.abc.exe becomes setup[1].abc.exe
$iefilename = preg_replace('/\./', '%2e', $fn, substr_count($fn, '.') - 1);
header("Content-Disposition: inline; filename=\"$iefilename\"");
}
else
{
header("Content-Disposition: inline; filename=\"$iefilename\"");
}
header("Accept-Ranges: bytes");
$range = 0; // default to begining of file
$speed = DS_SPEED; // speed in kb/s
$size=filesize($fil);
//check if http_range is set. If so, change the range of the download to complete.
if(isset($_SERVER['HTTP_RANGE']))
{
list($a, $range)=explode("=",$_SERVER['HTTP_RANGE']);
str_replace($range, "-", $range);
$size2=$size-1;
$new_length=$size-$range;
header("HTTP/1.1 206 Partial Content");
header("Content-Length: $new_length");
header("Content-Range: bytes $range$size2/$size");
}
else
{
$size2=$size-1;
header("Content-Range: bytes 0-$size2/$size");
header("Content-Length: ".$size);
}
//check to ensure it is not an empty file so the feof does not get stuck in an infinte loop.
if ($size == 0 ) sendToBrowser('Zero byte file! Aborting download');
set_magic_quotes_runtime(0); // in case someone has magic quotes on. Which they shouldn't as good practice.
// we should check to ensure the file really exits to ensure feof does not get stuck in an infite loop, but we do so earlier on, so no need here.
$fp=fopen("$fil","rb");
//go to the start of missing part of the file
fseek($fp,$range);
//start download
while(!feof($fp) and (connection_status()==0))
{
//change time limit so big files do not get cut off with a timeout.
set_time_limit(0);
print(fread($fp,1024*$speed));
flush();
ob_flush();
sleep(1);
}
fclose($fp);
if (DS_DLON ==1) writeLog($p,$fil); // Txt file detail logging.
if (DS_COUNTON == 1) countLog($fil); // Text file qty logging.
if (DB_ON == 1) dbLog($fil); // Database qty logging.
return((connection_status()==0) and !connection_aborted());
}