简体   繁体   中英

Failed to load PDF document in my PHP code

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM