2017-09-03 56 views
0

我想強制下載存儲在服務器上的pdf文件。如何強制下載存儲在服務器上的pdf文件?

我到目前爲止的嘗試導致損壞的pdf文件。

<div class="w3-row"> 
    <div class="w3-col l8 s12"> 
    <div class="w3-card-4 w3-margin w3-white "> 
     <div class="w3-display-container"> 
     <center> 
      <header class="w3-teal w3-center"> 
      <h2><b> You can download you syllabus here </b></h2> 
      </header> 
      <br> 
      <h2 class="w3-container " style="color: darkblue;"><strong><?php echo $branch ?></strong></h2> 
      <h3 class="w3-container"><strong><?php echo $sem ?></strong></h3> 

      <br> 
      <a class="w3-text-red w3-large" href="script/download.php?filedir='<?php echo " 
      script/ ".$dir ?>&filename= <?php echo $name ?> ' "><strong>Download</strong></a> 
      <br> 
      <br> 
     </center> 
     </div> 
    </div> 
    <hr> 
    </div> 
下面

是的download.php

 $path = $_GET['filedir'] ; 
     $filename = $_GET['filename']; 


     header('Connection: Keep-Alive'); 
     header('Content-Description: File Transfer'); 
     header('Content-Type: application/octet-stream'); 
     //header('Content-Disposition: attachment; 
     filename="'.basename($filename).'"'); 
     header('Content-Transfer-Encoding: binary'); 
     header('Expires: 0'); 
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
     header('Pragma: public'); 
     header('Content-Length: ' . filesize($filename)); 
     ob_clean(); 
     flush(); 
     readfile($path); 
+1

您可以顯示'download.php'相關的代碼文件名? –

+0

如果您將向我們顯示download.php文件代碼,那麼我們可以幫助您 –

+0

當然,我只是發佈它 –

回答

0

你有問題,在這裏,當你使用basename

$path = $_GET['filedir'] ; 
$filename = $_GET['filename']; 

header('Connection: Keep-Alive'); 
header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
//header('Content-Disposition: attachment; 
$filename=basename($filename); //change this line; 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($filename)); 
ob_clean(); 
flush(); 
readfile($path); 
+0

但先生,它現在正在下載download.php文件。爲什麼? –

相關問題