php
  • pdf
  • download
  • 2017-04-04 113 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); 
    } 
    ?> 
    

    希望它帮助那些像我这样的新手。

    快乐编码!

    相关问题