• 0

PHP download file thingy


Question

Hey, I'm just trying to have a simple php download script (http://www.yourdomain.com/download.php?file=something.zip). The code I've come upon is working but you can link to it from other sites so that defeats its purpose... heres the code i got so far...

<?
$dir="/path/to/file";
if (isset($_REQUEST["file"])) {
  $file=$dir.$_REQUEST["file"];
  header("Content-type: application/force-download");
  header("Content-Transfer-Encoding: Binary");
  header("Content-length: ".filesize($file));
  header("Content-disposition: attachment; filename=\"".basename($file)."\"");
  readfile("$file");
} else {
  echo "No file selected";
}
?>

How can i restrict it to a certain domain? and is there anychance to do this: when a certain ip/username downloads somethign says its 200meg and the ip comes back on the SAME day to download another 200meg can the script say no ya cnt download you have allready used up ur 200meg download limit for today try again tomorrow

Link to comment
https://www.neowin.net/forum/topic/114540-php-download-file-thingy/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

<?

$filename = $_GET["file"];

if(file_exists($filename)){ 

header("Content-type: application/octet-stream"); 
header("Content-Length: ".filesize($filename)); 
header("Content-Disposition: attachment; filename=$filename"); 

$fp = fopen($filename, 'rb'); 
fpassthru($fp); 
fclose($fp);

}else{
echo "File not found on server!";
}

?>

Usage: http://www.domain.com/download.php?file=mytextfile.txt

Try that, i made that about 2 months ago and never had to need it but just pulled it back up... im almost certain it works ;)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.