dqc22586 2016-03-23 00:49
浏览 11

当应该下载不同的文件时,单击具有不同ID的按钮会下载相同的文件

This is what I tried so far,when I click on button 1 it downloads some.txt. When I click on button 2 the same thing happens instead of downloading blue.txt i get some.txt again. In my table when the user clicks on button 2 they should be able to download the blue.txt file only.

    <?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>

</div>
  • 写回答

1条回答 默认 最新

  • dongshushen4392 2016-03-23 01:03
    关注

    You need put links or another script to download that files, the headers are send one time when your page is loaded for the browser.

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

    download.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;
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大