1

I am new to PHP and trying to implement below functionality through code give

Functionality: I have one Download button and upon clicking i should be able to download my .pdf file stored under htdocs-->xxfilename-->abc.pdf;

Code: I am using below code in my xyz.php web page

<?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';
?>

Error: My pdf file is getting downloaded but upon opening it it says "failed to load pdf document"

Please help where i am wrong.

***Edit**** I tried different approach and my file is downloading but still it says "failed to load pdf document"

My other approach code is below

<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);
        }
    }
    ?>

Please tell me if i am doing anything wrong.

  • It is very ugly way of implementation. – Amit Apr 04 '17 at 00:27
  • Looks like there is accepted answer for a similar question here http://stackoverflow.com/questions/1004478/read-pdf-files-with-php – Ryan Tuosto Apr 04 '17 at 00:37
  • @Amit: Could you suggest any better way of doing it? Please note, i have common template for all my pages. and in one of Page i need to show Download PDF button to download PDF file. – Tushar Kashyap Apr 04 '17 at 00:43
  • Possible duplicate of [How to make PDF file downloadable in HTML link?](http://stackoverflow.com/questions/364946/how-to-make-pdf-file-downloadable-in-html-link) – Mocking Apr 04 '17 at 01:44

1 Answers1

0

I implemented another solution after doing some more study and i was able to solve the problem.

Below is my code i used to implement.

Concept:

1) We can use HTML+JSP+PHP to get this functionality.

2) At HTML + JSP side we need to call eventListener to capture the event of clicking the button.

3) This event will redirect to the php page which will download the pdf file to your local computer.

Code Snippet:(here XYZ is the name of page where you want to show Download button)

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

in 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);
}
?>

Hope it help some one out there newbie like me.

Happy Coding!