<?php
/*
 * use this script to enable your visitors to download
 * your files. 
 */
 
$fileserver_path = 'K:/webfiles/';	// change this to the directory your files reside
$req_file 		 = $_GET['file'];
$whoami			 = "/dms/download.php";	// you are free to rename this file 

if (empty($req_file)) {
	print "Usage: $whoami?file=&lt;file_to_download&gt;";
	exit;
}

/* no web spamming */
/*if (!preg_match("/^[a-zA-Z0-9._-]+$/", $req_file, $matches)) {
	print "I can't do that. sorry.";
	exit;
}*/

/* download any file, but not this one */
if ($req_file == $whoami) {
	print "I can't do that. sorry.";
	exit;
}

// check to see if permission is needed / granted
/*if(strpos($req_file, "private/") === 0) {
  $token = $_GET['token'];
  if(!$token) {
    print "Permission denied";
    exit;
  }
  //print base64_encode(time());
  //exit;
  //decode token
  $token = base64_decode($token);
  if(!(time() - (60*60*24) < $token  && $token < time() + (60*60*24))) {
    print "<br />This link has expired.";
    exit;
  }
}*/

/* check if file exists */
if (!file_exists("$fileserver_path/$req_file")) {
	print "File <strong>/$req_file</strong> doesn't exist.";
	exit;
}

header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Length: ' . filesize("$fileserver_path/$req_file"));
header('Content-Disposition: attachment; filename=' . basename($req_file));
readfile("$fileserver_path/$req_file");
exit;

?>
