Customized File Download in PHP

<?
if($_GET['action']==”download”){
$file = “./images/”.$_GET['filename'].”.”;
header(“Content-type: application/force-download”); //optional
header(“Content-Transfer-Encoding: Binary”);
header(“Content-length: “.filesize($file));
header(“Content-Disposition: attachment; filename=”.basename($file).”");
readfile(“$file”);
}else{
$folder = “./images/”;
if (is_dir($folder)){
$contents = scandir($folder);
$bad = array(“.”, “..”, “.DS_Store”, “_notes”, “Thumbs.db”);
$files = array_diff($contents, $bad);
}
echo<<<FILES
?>
<table>
<?
FILES;
while(list($key,$value) = each($files)){
$rCtr++;
echo<<<FILES
?>
<tr><td align=”right”>$value</td>
<td><img src=”images/download.png”
onmousedown=”window.open(‘$PHP_SELF?action=download&filename=$value’,'_self’)”
style=”cursor:pointer” /></td></tr>
<?
FILES;
}
?>
</table>
<?
}
?>

<?

if($_GET['action']==”download”){

$file = “./images/”.$_GET['filename'].”.”;

header(“Content-type: application/force-download”); //optional

header(“Content-Transfer-Encoding: Binary”);

header(“Content-length: “.filesize($file));

header(“Content-Disposition: attachment; filename=”.basename($file).”");

readfile(“$file”);

}else{

$folder = “./images/”;

if (is_dir($folder)){

$contents = scandir($folder);

$bad = array(“.”, “..”, “.DS_Store”, “_notes”, “Thumbs.db”);

$files = array_diff($contents, $bad);

}

echo<<<FILES

?>

<table>

<?

FILES;

while(list($key,$value) = each($files)){

$rCtr++;

echo<<<FILES

?>

<tr><td align=”right”>$value</td>

<td><img src=”images/download.png”

onmousedown=”window.open(‘$PHP_SELF?action=download&filename=$value’,'_self’)”

style=”cursor:pointer” /></td></tr>

<?

FILES;

}

?>

</table>

<?

}

?>

Leave a Reply

Your email address will not be published. Required fields are marked *

*