Accessed as such: http://localhost/forcedownload.php?file=../images/mustdownload.jpg
<?php
$filename = $_GET[’file’];
//check for existence of file first if not there die.
if(!file_exists($filename))
{
exit();
//for testing:
//die(’Error: File not found.’);
}
//could be used to test to make sure that only files from a particular directory are accessed
$baseurl = str_replace($DOCUMENT_ROOT, ”, dirname($filename));
$fileextension = strtolower(substr(strrchr($filename,’.'),1));
//make sure that we are only trying to download files with an image extension. An array of MIME types and loop checking might be the way to go in the future.
if ($fileextension!=’jpg’ && $fileextension!=’gif’ && $fileextension!=’png’)
{
exit();
//for testing:
//die(’Error: Not an image file like intended’);
}
// required for IE, otherwise Content-disposition is ignored
if(ini_get(’zlib.output_compression’))
{
ini_set(’zlib.output_compression’, ‘Off’);
}
header(”Pragma: public”); // required
header(”Expires: 0″);
header(”Cache-Control: must-revalidate, post-check=0, pre-check=0″);
header(”Cache-Control: private”,false);
header(”Content-Type: image/jpg”);
header(”Content-Disposition: attachment; filename=”.basename($filename).”;” );
header(”Content-Transfer-Encoding: binary”);
header(”Content-Length: “.filesize($filename));
readfile(”$filename”);
exit();
?>
No Comment Received
Leave A Reply