php
  • pdf
  • download
  • 2017-04-04 52 views 1 likes 
    1

    我是新來的PHP,並試圖通過代碼來實現以下功能給無法加載PDF文檔中我的PHP代碼

    功能: 我有一個下載按鈕,在點擊我應該能夠下載我的.pdf文件存儲在htdocs下 - > xxfilename - > abc.pdf;

    代碼: 我使用下面的代碼在我的xyz.php網頁

    <?php 
    $title = "Learning"; 
    $content = ' 
          <h3> Intro</h3> 
          <p> 
           Introduction goes here 
          </p> 
          <form action = "xyz.php" method = "post" name = "downloadform"> 
           <input type="submit" value="Download" name="dwnld_file"/> 
          </form> 
         '; 
    if (isset($_POST['dwnld_file'])) { 
        mysql_connect("localhost", "root", ""); 
        mysql_select_db("PQR"); 
        $res = mysql_query("Select * from tab1"); 
        while ($row = mysql_fetch_array($res)) { 
         $file = '$row["col2"]'; 
         header('Content-Type: application/pdf'); 
         header('Content-Disposition: attachment; filename="' . $row["col2"] . '"'); 
         header('Content-Transfer-Encoding: binary'); 
         header('Accept-Ranges: bytes'); 
         header('Content-Length:' . filesize($file)); 
         readfile($file); 
        } 
    } 
    include 'Template.php'; 
    ?> 
    

    錯誤: 我的PDF文件是越來越下載,但在打開它,它說「無法加載PDF文檔」

    請幫我在哪裏錯了。

    ***編輯**** 我嘗試不同的方法,我的文件被下載但仍說「無法加載PDF文檔」

    我的另一種方法的代碼如下

    <form action = "xyz.php" method = "post" name = "downloadform"> 
         <input type="submit" value="Download " name="dwnld_file"/> 
        </form> 
        <?php 
        if (isset($_POST['dwnld_file'])) { 
         mysql_connect("localhost", "root", ""); 
         mysql_select_db("test"); 
         $res = mysql_query("Select * from tab1"); 
         while ($row = mysql_fetch_array($res)) { 
          $file = $row["col1"]; 
          echo $file; 
    
          header('Content-Type: application/pdf'); 
          header('Content-Disposition: attachment; filename="' .$file. '"'); 
          header('Content-Transfer-Encoding: binary'); 
          header('Accept-Ranges: bytes'); 
          header('Content-Length:' . filesize($file)); 
          readfile('myfile/'.$file); 
         } 
        } 
        ?> 
    

    請告訴我,如果我做錯了什麼。

    +0

    這是非常醜陋的執行方式。 – Amit

    +0

    看起來像是有一個類似的問題在這裏接受的答案http://stackoverflow.com/questions/1004478/read-pdf-files-with-php –

    +0

    @Amit:你可以建議任何更好的方式嗎? 請注意,我有我的所有網頁的通用模板。並在頁面中的一個我需要顯示下載PDF按鈕下載PDF文件。 –

    回答

    0

    我做了一些更多的研究後,我實現了另一種解決方案,我能夠解決這個問題。

    下面是我用來實現的代碼。

    概念:

    1)我們可以使用HTML + JSP + PHP來獲得此功能。

    2)在HTML + JSP端,我們需要調用eventListener來捕獲點擊按鈕的事件。

    3)此事件將重定向到php頁面,該頁面將pdf文件下載到本地計算機。

    代碼段:(這裏XYZ是要顯示下載鏈接頁面的名稱)

    在XYZ.php

    <input type="button" id="btnshow" value="Download" name="dwnld" /> 
         <script> 
          var btn = document.getElementById('btnshow'); 
          btn.addEventListener('click', function() { 
           document.location.href = '<?php echo 'download.php'; ?>' 
          }) 
         </script> 
    

    中的download.php

    <?php 
    mysql_connect("localhost", "root", ""); 
    mysql_select_db("database_name"); 
    $res = mysql_query("Select * from table_name"); 
    while ($row = mysql_fetch_array($res)) { 
        $file = 'Pdf_File/' . $row["Path"]; 
        $filename = $row["Path"]; 
        header('Content-Type: application/pdf'); 
        header('Content-Disposition: attachment; filename="' . $filename . '"'); 
        header('Content-Transfer-Encoding: binary'); 
        header('Accept-Ranges: bytes'); 
        readfile($file); 
    } 
    ?> 
    

    希望它幫助那些像我這樣的新手。

    快樂編碼!

    相關問題