我有一个项目,我的用户可以下载文件(pdf,docs,txt,图像等)。所以我提供了这样的下载链接 -
<li><a href="home.php?search=<?php echo $search_string; ?>&search_submit=Search&page=<?php echo $page; ?>&download=<?php echo $result_materialsID; ?>">
Download</a></li>
然后我检查了我的$ _GET方法我检查了这样的条件 -
if(isset($_GET['download']))
{
$material_to_download = $_GET['download'];
$result_update_materials = mysqli_query($connection,"Update Materials SET numDownload=(numDownload+1) where materialsID = {$_GET['download']}");
if(!$result_update_materials)
{
die("Database update numDownload materials failed:".mysqli_error($connection));
}
$result_get_materials_l = mysqli_query($connection,"SELECT downloadLink FROM Materials where materialsID = {$_GET['download']}");
if(!$result_get_materials_l)
{
die("Database get materials failed:".mysqli_error($connection));
}
else
{
while($row = mysqli_fetch_array($result_get_materials_l))
{
download_file($row[0]);
}
}
}
它是在嵌套ifs下我检查其他$ _GET变量。函数download_file如下:
function download_file($file)
{
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
}
}
问题:问题是,当我点击下载链接时,文件下载正常,并且随着代码的推移,我的数据库应该将numDownload增加1.但它增加了2. numDownload跟踪该文件的下载总数。所以我认为我的代码的更新部分实际上运行了两次。
我尝试了unset($_GET['download'])
,但没有帮助。知道出了什么问题吗?感谢。