2016-03-23 2 views
0

Это то, что я пробовал до сих пор, когда я нажимаю кнопку 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=='') { 
 
\t $id = $_SESSION["id"]; 
 
\t $time = Now(); 
 
\t $insert = $con->query("INSERT INTO my_table (LoggedIn) Values($time) WHERE id = '$id'"); 
 
} 
 

 
else{ 
 
\t $id = $_SESSION["id"]; 
 
\t $update = $con->query("UPDATE my_table SET LoggedIn = Now() WHERE id = '$id'"); 
 
} 
 

 
\t 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 --> 
 
\t <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> 
 
\t <!--<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"> 
 
\t \t \t <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> 
 
\t <br/> 
 
\t <br/> 
 
\t <br/> 
 
\t <br/> 
 
\t <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"> 
 
\t <thead> 
 
\t <tr> 
 
\t  <th style="text-align:center;">FileID</th> 
 
\t \t <th style="text-align:center;">FileName</th> 
 
\t \t <th style="text-align:center;">Download File</th> 
 
\t </tr> 
 
\t </thead> 
 
\t <tbody> 
 
<?php 
 

 
$res = $con->query("select * from File_Table"); 
 
while ($row = $res->fetch_assoc()) { 
 
?> 
 
    
 
\t <tr> 
 
\t  <td><?php echo $row['FileID']; ?></td> 
 
\t \t <td><?php echo $row['FileName']; ?></td> 
 
\t \t <td> 
 
\t \t <form action="user.php" method="post"> 
 
\t \t <input type="submit" name="Download" id="<?php echo $row['FileID'];?>" value="<?php echo $row['FileID'];?>"> 
 
<?php 
 
\t \t $file_row = $row['FileID']; echo $file_row; $file_name = $row['FileName']; echo $file_name; 
 
\t \t if(isset($_POST['Download'])){ 
 
\t 
 
\t $results = $con->query("select * from File_Table where FileID = '$file_row'"); 
 
if($rows = $results->fetch_assoc()){ 
 
\t $uploading = $rows['FileName']; 
 
\t echo $uploading; 
 
\t $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; 
 
} 
 
} 
 
} 
 
?> 
 
\t \t </form> 
 
\t \t </td> 
 
\t </tr> 
 
<?php 
 
} 
 
?> 
 
\t </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>

+0

Sidenote: Это подведет '$ времени = Now();'. Вы получите неопределенное постоянное уведомление. –

+0

спасибо за головы –

ответ

0

Вам нужно положить ссылки или другой скрипт для загрузки файлов, что заголовки посылают один раз, когда ваша страница загружается в браузере.

<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; 
     } 
    } 
} 
+0

$ _POST ['Скачать'] <-where это пришло от –

+0

Я обновил код, извините –

+0

Спасибо, я скоро закрою его –

Смежные вопросы