I have written the a download script which will download a file from a directory. Upon successful download I need to update the database so I write the following code.
$path = $_SERVER['DOCUMENT_ROOT']."/upload/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_file'];
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);
$update = mysql_query("Update query");
if($update) {
echo "updated";
}
else {
echo 'error'.mysql_error();
}
exit;
But when I click on the download link and a browser popup appears the in the popup when I click on the cancel button, it should not execute the update query as the file is not downloaded but when in using the above code even if I click on the cancel button the update query executed.
So what is the mistake in my code?