2

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?

Ahmad
  • 2,099
  • 10
  • 42
  • 79

1 Answers1

3

You will need to check to see if the user aborted the request, and if not, only then insert the row.

if (connection_status() == CONNECTION_NORMAL) {
    // do query here
}

See:

Petah
  • 45,477
  • 28
  • 157
  • 213
  • I put my update query in `if (connection_status() != CONNECTION_NORMAL) {}` but now if the file downloaded successfully the update query does not execute. – Ahmad May 07 '12 at 10:37
  • @Ahmad, wups sorry I guess it should be `if (connection_status() == CONNECTION_NORMAL)` – Petah May 07 '12 at 10:39
  • I checked this also. When I cancel the browser download window the update query get executes. – Ahmad May 07 '12 at 10:41
  • @Ahmad try setting `ignore_user_abort(true)` as well (up the top of yours script), also how big is the file/how long does it take to download? – Petah May 07 '12 at 10:45
  • I used the `ignore_user_abort(true)` on the top of the script and then try but even if I cancel the browser download window the update query executed. The file is in KBs and it download the file in no time. – Ahmad May 07 '12 at 13:01
  • @Ahmad well it seems like you are outputting the data too fast, it has probably already been sent by PHP and cached on the network interface. – Petah May 07 '12 at 19:42
  • Then what is the solution of my problem? – Ahmad May 08 '12 at 05:56
  • @Ahmad it cannot be done (or is extremely complicated) with the technology you are using, – Petah May 08 '12 at 10:58