I am working in a environment dealing with all sorts of files that need to be open from a central location dynamically??
I have posted the code below so you can see where I am coming from.... The problem I am having is the SQL to update the log table somehow is being execute twice in this script.
I have looked all over the place and cannot find one solution.
Here is the code:
function show_file($id_document="",$filename="",$method) {
global $cfg,$__CONNECTION,$data;
// Retrieve filename from database if id_document is sent to function
if( isset($id_document) ) {
$sql = "SELECT filename FROM ". TBL_LIB ." WHERE id_subacct=". $data['id_subacct'] ." AND id_document = '". $id_document ."'";
$__CONNECTION->query($sql);
$num_docs = $__CONNECTION->get_num_rows();
unset($sql);
// Check to see if valid document
if( $num_docs == 1 ) {
$row = $__CONNECTION->get_fetch_array();
$filename = $row['filename'];
} else {
header("Location: ". PAGE_ERROR_LIBRARY . "?errorno=1007");
exit();
}
}
// **************************************
// PROBLEM HERE
// **************************************
// Increment Number of Times Opened Field
$sql = "INSERT INTO ". TBL_LOG_DOC ." (id_subacct,id_document,opened) VALUES (".$data['id_subacct'].",".$id_document.",now());";
$__CONNECTION->query($sql);
unset($sql);
if( file_exists($cfg['file_path'].$filename) ) {
// Set headers
header('Content-Length: '.filesize($cfg['file_path'].$filename));
//header('Content-Transfer-Encoding: binary');
// Get filetype
$filetype = get_filetype($filename);
if( array_key_exists($filetype,$cfg['content_type']) ) {
header('Content-Type: '.$cfg['content_type'][$filetype]);
}
else {
header('Content-Type: application/force-download');
}
//header('Referer: '.PAGE_LIBRARY);
switch($method) {
case "view":
header('Content-Disposition: inline; filename="' . $filename . '"');
break;
case "download":
header('Content-Disposition: attachment; filename="' . $filename . '"');
break;
default:
header('Content-Disposition: filename="' . $filename . '"');
break;
}
// Open the file
$buffer = "";
$handle = fopen($cfg['file_path'].$filename,"r");
while( !feof($handle) ) {
$buffer .= fread($handle,filesize($cfg['file_path'].$filename));
}
fclose($handle);
echo $buffer;
//readfile($cfg['file_path'].$filename);
exit();
} else {
header('Location: '.PAGE_ERROR_LIBRARY.'?errorno=1007');
exit();
}
return;
}