Prompt user to download PDF file instead of opening

后端 未结 6 2101
庸人自扰
庸人自扰 2021-01-05 07:14

In my project site, if I click on a link, the PDF opens in a new or parent window. Well I want a box to appear that prompts the user to download the file instead of opening

6条回答
  •  甜味超标
    2021-01-05 07:40

    If you are getting a corrupted file error try this:

    header('Pragma: public');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Cache-Control: private', false);
    header('Content-Type: application/pdf');
    header('Content-disposition: attachment; filename=' . basename($file));
    header("Content-Transfer-Encoding:  binary");
    header('Content-Length: ' . filesize($file)); // provide file size
    header('Connection: close');
    readfile($file);
    

    Where $file is the full path or url of the file.

提交回复
热议问题