2017-09-20 14 views
0
<?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; 
     } 
    } 
} 
?> 

答えて

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ループの外にファイルのダウンロードコードを記述することもその優れたEDITを使用して、それを受信する必要があります。

// 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>"; 
0

実際にファイルをダウンロードとして出力するコードは、foreachループ内にあります。チェックif (isset($_GET['download']))が合格した場合は、最初の反復でそれを行い、その反復でファイルを出力します。これを修正する方法の1つは、チェックを次のように拡張することです:

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

関連する問題