query("SELECT * FROM $table WHERE filename=\"$filename\"");
if($stmt->rowCount() > 0){
// File has entry in database, update it
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$id = $row["id"];
$nhits = $row["nhits"]+1;
$query = "UPDATE $table SET nhits='$nhits' WHERE id=$id";
}else{
$query = "INSERT INTO $table (filename,nhits,last_reset,created,modified)";
$query .= " VALUES(\"$filename\",'1',NOW(),NOW(),NOW())";
}
$dbh->query($query);
//print $query."
\n";
//die();
} catch (PDOException $e) {
print "Error connecting to to database : ".$e->getMessage();
die();
}
//------------------------ Send file to user --------------------------
// (the following was taken from http://www.finalwebsites.com/forums/topic/php-file-download on 1/11/2011)
// place this code inside a php file and call it f.e. "download.php"
$path = dirname(__FILE__);
//$path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure
$fullPath = $path."/$filename";
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
fclose ($fd);
}else{
print "File \"$filename\" does not exist in \"$path\"!
";
}
?>