<?php

function download#primaryfield##record#(){
	$#lookupkey#=GETVAL('#lookupkey#');
	
	global $db;
	
	$query="select * from #lookuptable# where #lookupkey#=?";
	$rs=sql_prep($query,$db,$#lookupkey#);
	if (!$myrow=sql_fetch_array($rs)) die('Missing Document #'.$#lookupkey#);
	
	$fn='../../#primaryfield##records#/'.$#lookupkey#.'#fileext#';
	$docfn=$myrow['#dispfield#'];
	
	$ofn=str_replace(' ','_',$myrow['ofn']);
	$mimetype=$myrow['mimetype'];


	/*	
	// Direct View:
	
	header('Content-Type: '.$mimetype);
	
	*/	
		
		
	// Force Download:
		
	header('Content-Type: application/octet-stream');
	header("Content-disposition: attachment; filename=$ofn");
	

	clearstatcache();
	echo file_get_contents($fn);
	die();	
}