亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP下載腳本錯誤加載PDF文檔失敗

PHP下載腳本錯誤加載PDF文檔失敗

PHP
米琪卡哇伊 2021-11-05 13:27:12
我有一個簡單的下載腳本,可以在下面找到:if (isset($_GET['name'])){        $name = $_GET['name'];        $stmt = $conn->prepare("SELECT link_to_policy FROM policies WHERE name = ? LIMIT 1");        $stmt->bind_param('s', $name);        $stmt->execute();        $result = $stmt->get_result();        $result2 = $result->fetch_assoc();        $policytodownload = $result2["link_to_policy"];        if (file_exists($policytodownload)) {            header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");            header("Content-Type: application/msword");            header("Content-Type: application/pdf");            header('Content-Disposition: attachment');            header("Cache-Control: no-cache");            header('Content-Length: ' . filesize($policytodownload));            ob_clean();            flush();            readfile($policytodownload);            exit;        }       }   link_to_policy 列包含保存策略的文件的完整路徑。點擊鏈接后,文件被下載,但點擊文件后,即使是word文檔,我在Chrome中收到錯誤信息(所有PDF文件都是在那里打開的):Failed to load PDF文檔。你能幫我么?謝謝!
查看完整描述

1 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

不要發送多個Content-type標頭,發送文件擴展名的適當類型。


if (isset($_GET['name'])){


    $name = $_GET['name'];


    $stmt = $conn->prepare("SELECT link_to_policy FROM policies WHERE name = ? LIMIT 1");

    $stmt->bind_param('s', $name);

    $stmt->execute();

    $result = $stmt->get_result();

    $result2 = $result->fetch_assoc();


    $policytodownload = $result2["link_to_policy"];

    if (file_exists($policytodownload)) {

        $type_map = ['xls' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',

                     'doc' => 'application/msword',

                     'pdf' => 'application/pdf'];

        $ext = pathinfo($policytodownload, PATHINFO_EXTENSION);

        if (isset($type_map[$ext])) {

            header("Content-Type: {$type_map[$ext]}");

        }

        header("Cache-Control: no-cache");

        header('Content-Length: ' . filesize($policytodownload));

        ob_clean();

        flush();

        readfile($policytodownload);

        exit;

    }   

}


查看完整回答
反對 回復 2021-11-05
  • 1 回答
  • 0 關注
  • 273 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號