我编写了一个下载脚本,它将从目录下载文件.
下载成功后,我需要更新数据库,以便编写以下代码.
$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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。