我编写了一个脚本,当用户想要下载文件时,它首先显示广告然后开始下载,通过$ _GET传递文件的ID。 问题是,如果我到达没有指定ID的页面(例如download_file.php而不是download_file.php?id = 1),页面就开始下载页面本身。
<?php
require("/membri/lostlife/mysql.php");
// Variables:
$id = $_GET["id"];
$result = mysql_query("SELECT * FROM Setting WHERE ID = $id");
$row = mysql_fetch_array($result);
$downloads = $row["Downloads"] + 1;
//
switch ($_GET["action"])
{
case "download":
// Download the file:
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"$row[Filename]\"");
readfile("/membri/lostlife/setting/$row[Filename]");
// Update the database:
mysql_query("UPDATE Setting SET Downloads = $downloads WHERE ID = $id");
break;
default:
echo "";
header("Refresh: 5; url=?id=$id&action=download");
}
?>
这是我的代码。怎么了?
答案 0 :(得分:0)
此外,您的交换机默认为刷新标头..所以当操作不是'下载'时,它将刷新到action = download。
生病会这样做:require("/membri/lostlife/mysql.php");
$id = $_GET["id"];
$action = $_GET["action"];
// if its not empty and it is numeric(check if its a integer can be done in different ways)
if(!empty($id) && is_numeric($id))
{
$query = mysql_query("SELECT Downloads, Filename FROM Setting WHERE ID = $id");
$row = mysql_fetch_assoc($query);
$download = $row['Downloads'];
$filename = $row[Filename];
if($action == "downoad") {
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"". $filename ."\"");
readfile("/membri/lostlife/setting/". $filename);
}
}
else
{
die("No ID found");
};
您还要更新某些内容吗?您所知道的是更新下载您从select语句中获得的内容?所以你不需要更新它?你想要计算下载的数量吗?