gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:47:38 25 4
gpt4 key购买 nike

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

$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
  • 关于php - 下载成功后如何更新数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10480125/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com