php下载页面继续获取相同的文件

时间:2017-09-20 07:19:09

标签: php

<?php
foreach ($courserows AS $courserow) {
    echo "  <tr>";
    echo '<td>' . $courserow['Name'] . '</td>';
    echo '<td>' . $courserow['Description'] . '</td>';
    ?>
    <td>
        <a href='level-details.php?LId=<?php echo $onecourse['levelid'];?> &download' class='btn btn-success'>
            تحميل <i class="fa fa-download" aria-hidden="true"></i>
        </a>
    </td>
    <?php
    echo "</tr>";

    // download file
    if (isset($_GET['download'])) {
        $file = 'material/' . $courserow['file'];
        echo $file;

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

2 个答案:

答案 0 :(得分:1)

您需要在单独的文件中编写文件下载代码。  但是,根据您的代码,您需要在锚标记中传递文件名并使用$_GET

接收它
<?php foreach ($courserows AS $courserow){

  echo "  <tr>";
  echo '<td>'.$courserow['Name'].'</td>';

  echo '<td>'.$courserow['Description'].'</td>';



       ?>
       <td>

           <a href='level-details.php?file=<?php echo $onecourse['file'];?>&download' class='btn btn-success'>تحميل   <i class="fa fa-download" aria-hidden="true"></i></a>


       </td>
       <?php
       echo "</tr>";

       // download file
       if (isset($_GET['download'])) {
           $file = 'material/' . $_GET['file'];
           echo $file;


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

编辑:最好在foreach循环之外编写文件下载代码。

// download file
 if (isset($_GET['download'])) {
     $file = 'material/' . $_GET['file'];
     echo $file;


     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;
     }
 }
foreach ($courserows AS $courserow){

  echo "  <tr>";
  echo '<td>'.$courserow['Name'].'</td>';

  echo '<td>'.$courserow['Description'].'</td>';



       ?>
       <td>

           <a href='level-details.php?file=<?php echo $onecourse['file'];?>&download' class='btn btn-success'>تحميل   <i class="fa fa-download" aria-hidden="true"></i></a>


       </td>
       <?php
       echo "</tr>";

答案 1 :(得分:0)

实际输出文件作为下载的代码在foreach循环中。如果检查if (isset($_GET['download']))通过,它将在第一次迭代时执行,并在该迭代中输出文件。解决此问题的一种方法是将支票扩展为:

if (isset($_GET['download']) && $_GET['download'] == $courserow['file'])