简体   繁体   中英

Problem Opening a PDF File with Adobe Reader That Is Downloaded from MySQL database via PHP

I have written some PHP code that downloads a PDF file from a MySQL database on a server. The code works great and I can view/download/open the PDF file from various browsers. However, when I download the file and try to open up via Adobe Reader I get a message that says the file could not be opened because "because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)"

I am sure it is something dumb that I am forgetting to do in my code. Below is the PHP code that I am using:

// Downloads files
if (isset($_GET['scenario_id'])) {
    $scenario_id = $_GET['scenario_id'];

    // fetch file to download from database
    $sql = "SELECT * FROM  market_plans_pdfs WHERE scenario_id=$scenario_id";
    $result = mysqli_query($connect, $sql);

    $file = mysqli_fetch_assoc($result);
    $pdf_file=$file['pdf_file'];


    $filename=$file['scenario_name'];


    if (isset($filename)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/pdf');
        header('Content-Disposition: inline; filename="'.basename($filename).'"');//set content-disposition as "inline" as opposed to "attachment" so that it opens first in the browser
        header('Content-Transfer-Encoding: binary');
        header('Accept-Ranges: bytes');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        @readfile("data:application/pdf;base64,$pdf_file");
    }

}

Any help would be appreciated as I have tried various solutions online with no success.

When you are changing some headers you have to do it before any output, because header must be sent at the beginning, for example:

This is invalid:

echo 'Test';
header('Content-Description: File Transfer');

Thats not your case, but you do similar mistake, on the other end.

When you are printing a file data, for example, as you do in your question, there is no function to tell that "your data ends here", just whole output is taken as file.

If you can't separate your file transfer from other output there is simple solution, try it out:

if (isset($filename)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/pdf');
    header('Content-Disposition: inline; filename="'.basename($filename).'"');//set content-disposition as "inline" as opposed to "attachment" so that it opens first in the browser
    header('Content-Transfer-Encoding: binary');
    header('Accept-Ranges: bytes');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    echo base64_decode($pdf_file);
    exit(); // nothing else will happen, no website which is shown otherwise
}

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