Well, I do this sort of thing on several secure webpages (https
. First I start a session and branch to a login page if the user has no cookie and the session has not expired. The Login page branches back to the calling page where the user is authenticated, and proceeds with the rest of the program. As part of the script, I list a directory contents in the page (excluding certain things that shouldn't be downloaded), with page rollover and checkboxes. The user selects the file(s) that he wants to download and presses submit, then the file name (from a list, and if a list, the file is zipped/archived - but that's a big complexity) is sent to a download function and forced to the user's computer. At the same time, I write the download transaction info to a logfile (the global $myFile).
The forced download function I use is this:
Code:
function f_download($file_name) {
global $myFile, $user;
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); } // required for IE
switch(strtolower(substr(strrchr($file_name,'.'),1)))
{
case 'pdf': $mime = 'application/pdf'; break;
case 'hdf': $mime = 'application/x-hdf'; break;
case 'zip': $mime = 'application/zip'; break;
case 'jpg': $mime = 'image/jpg'; break;
case 'png': $mime = 'image/png'; break;
case 'cdf': $mime = 'application/x-netcdf'; break;
case 'xls': $mime = 'application/octet-stream' ; break;
default: $mime = 'application/force-download';
}
ob_clean_all(); // clean output buffer before the file transfer set-up
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file_name)); // provide file size
readfile($file_name); // push it out
$fh = fopen($myFile,'a') or die('cannot open file: ' . $myFile); // open download log file
$stringData = date("Y/m/d H:i:s")." ".$user['user_name']." ".$_SERVER['REMOTE_ADDR']." downloaded ".$file_name."\n";
fwrite($fh, $stringData); // write download transaction
fclose($fh);
exit();
}
And the object clean function is this:
Code:
function ob_clean_all() {
$ob_active = ob_get_length() !== false;
while($ob_active) {
ob_end_clean();
$ob_active = ob_get_length() !== false;
}
return true;
}
Does this help?
Bookmarks