2017-09-20 1 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; 
     } 
    } 
} 
?> 

Antwort

1

Sie müssen Datei Download-Code in seoate Datei schreiben. Aber wie pro Ihren Code müssen Sie den Dateinamen in Anker-Tag übergeben und erhalten sie $ _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; 
      } 
     } 

EDIT auch seine bessere Verwendung der Datei-Download-Code außerhalb foreach-Schleife zu schreiben.

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

Der Code, der die Datei tatsächlich als Download ausgibt, befindet sich in der foreach-Schleife. Wenn die Prüfung if (isset($_GET['download'])) bestanden wird, wird dies bei der ersten Iteration der Fall sein und die Datei in dieser Iteration ausgeben. Eine Möglichkeit, dies zu beheben, besteht darin, die Prüfung auf etwas zu erweitern:

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