php – 成功下载后如何更新数据库?

我编写了一个下载脚本,它将从目录下载文件.
下载成功后,我需要更新数据库,以便编写以下代码.

$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;

但是当我点击下载链接并且当我点击取消按钮时弹出窗口中出现浏览器弹出窗口时,它不应该执行更新查询,因为文件没有下载但是在使用上面的代码时即使我点击取消按钮执行更新查询.

那么我的代码中的错误是什么?

解决方法:

您需要检查用户是否中止了请求,如果没有,则只需插入行.

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

看到:

> http://www.php.net/manual/en/function.ignore-user-abort.php
> http://www.php.net/manual/en/function.connection-status.php
> http://www.php.net/manual/en/function.connection-aborted.php

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...