单击具有不同id的按钮可以下载相同的文件,而应该下载不同的文件


Clicking on a button with different id downloads the same file when it should download a different file

这是我迄今为止所尝试的,当我点击按钮1时,它会下载some.txt。当我单击按钮2时,同样的事情发生了,而不是下载blue.txt,我再次得到some.txt。在我的表格中,当用户点击按钮2时,他们应该只能下载blue.txt文件。

    <?php
error_reporting(E_ALL & ~E_NOTICE);
include 'connection/connection.php';
session_start();
if(isset($_SESSION["id"])) {
$username = $_SESSION['username'];
$result = $con->query("select * from my_table where UserName='$username'");
$row = $result->fetch_assoc();
$log = $row['LoggedIn'];
if($log=='') {
	$id = $_SESSION["id"];
	$time = Now();
	$insert = $con->query("INSERT INTO my_table (LoggedIn) Values($time) WHERE id = '$id'");
}
else{
	$id = $_SESSION["id"];
	$update = $con->query("UPDATE my_table SET LoggedIn = Now() WHERE id = '$id'");
}
	if(isset($_POST['LogOut'])){
        header('Location:LogOut.php');
    }
}
/* need to add this later
else{
    header('Location: LogIn.php');
exit;
} */
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>User Api Docs</title>
    <!-- Bootstrap -->
	<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    
    <![endif]-->
</head>
<body>
	<!--<nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">API Docs User</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
			<li class="active"><a href="#">Home</a></li>
            <li><a href="LogOut.php" style="text-decoration:none;">Log Out</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>
	<br/>
	<br/>
	<br/>
	<br/>
	<div class="text-center">
Welcome! <?php echo $row['UserName']; ?>. You are logged in. Your UserID is <?php echo $row['id'];?>
<br/>
<br/>
<div class="container">
<table class="table table-bordered table-hover">
	<thead>
	  <tr>
	    <th style="text-align:center;">FileID</th>
		<th style="text-align:center;">FileName</th>
		<th style="text-align:center;">Download File</th>
	  </tr>
	</thead>
	<tbody>
<?php
$res = $con->query("select * from File_Table");
while ($row = $res->fetch_assoc()) {
?>
    
	  <tr>
	    <td><?php echo $row['FileID']; ?></td>
		<td><?php echo $row['FileName']; ?></td>
		<td>
		<form action="user.php" method="post">
		<input type="submit" name="Download" id="<?php echo $row['FileID'];?>" value="<?php echo $row['FileID'];?>">
<?php
		$file_row = $row['FileID']; echo $file_row; $file_name = $row['FileName']; echo $file_name;
		if(isset($_POST['Download'])){ 
	
	$results = $con->query("select * from File_Table where FileID = '$file_row'");
if($rows = $results->fetch_assoc()){
	$uploading = $rows['FileName'];
	echo $uploading;
	$file = 'docs_uploaded/'. $uploading;
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($file).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}
}
}
?>
		</form>
		</td>
	  </tr>
<?php
}
?>
	</tbody>
      </table>
</div>
<!-- jQuery (necessary for Bootstrap's Javascript plugins) -->
<script type="text/javascript" src="js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script type="text/javascript" src="bootstrap/js/bootstrap.js"></script>
</body>
</html>

您需要放置链接或其他脚本来下载这些文件,当您的页面加载到浏览器时,会一次性发送标题。

<form action="download.php" method="post" >
    <input type="submit" name="fileID" id="fileID" value="<?php echo $row['FileID']; ?>">
</form>

下载.php

<?php
//You need validate if your user user is logged on
if (isset($_POST['fileID']) && isset($_SESSION["id"])) {
    //Filter only int for avoid SQL injection or changed for prepared
    $iFile = filter_var($_POST['fileID'], FILTER_VALIDATE_INT);
    $results = $con->query("select * from File_Table where FileID = '$iFile'");
    if ($rows = $results->fetch_assoc()) {
        $uploading = $rows['FileName'];
        echo $uploading;
        $file = 'docs_uploaded/' . $uploading;
        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="' . basename($file) . '"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            readfile($file);
            exit;
        }
    }
}