force downloading files in PHP
In an interesting debate, a friend wanted to have a script that would make files to force download, like images, zips, exe to pdf’s. Here’s a nifty little script that would achieve just that. With a little creativity you can make it do more :p just save the following snippet as fetch.php and voila.
<?php
//load the file to the variable $file
$file = stripslashes($_GET['file']);
//function to do the downloading bit
function fetch($file){
//evaluating if the file is valid on size
if (filesize($file)<= 0) {
print "no files ";
}else {
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
}
}
//load the function
fetch($file);
?>
to use this simply call to it like this
<a href="fetch.php?file=leopard.iso">download</a>
as you can see its pretty simple and straightforward, like i said with a little creativity you can do validation on what type of files must be passed and what to not. as you can see in this script, there are alot of security issues - but hey this is just to give you guys an idea of how to force download files using PHP. If many people request a fully fledge script, i could create one
anyhoo thanks for the read ![]()