Using php to force download a pdf

后端 未结 5 1516
渐次进展
渐次进展 2020-12-17 21:25

Im trying to get a website to have a button that forces a download of a pdf.

Heres the html of the button:

    
           


        
相关标签:
5条回答
  • 2020-12-17 21:44

    I always use Gowon Patterson's download script, it also has hotlink protection: http://by.gowondesigns.com/getfile/

    0 讨论(0)
  • 2020-12-17 21:46
    $file_url = www.example.com/pdffolder/$pdfname;
    header('Content-Type: application/pdf');
    header("Content-Transfer-Encoding: Binary");
    header("Content-disposition: attachment; filename=".$pdfname);
    readfile($file_url);
    
    0 讨论(0)
  • 2020-12-17 21:46

    By the way, a bit late, but to identify the problem properly here:

    Your download script is at scripts/download.php and the file you want to download is at documents/[...].pdf.

    Therefore, your readfile() function should be traversing to the parent directory (outside of scripts/), e.g. readfile('../documents/[...].pdf');.

    0 讨论(0)
  • 2020-12-17 21:48

    Have you tried getting rid of the closing PHP tag (the ?>) at the end? It will treat the page as a pure PHP page, removing any possible new lines that might accidentally get appended to the end of the output. This helped me when I was dynamically creating excel files for download, and they were downloading as corrupted. Check out this page for more information:

    http://www.php.net/manual/en/language.basic-syntax.phptags.php

    From your edited question, it seems like PHP is unable to find the file. Try using an absolute path to the file like so: "c:\blah\de\blah\bloo.pdf" or "c:/blah/de/blah/bloo.pdf". If one of those paths works and downloads correctly, your relative path is incorrect in some way.

    0 讨论(0)
  • 2020-12-17 21:58

    Try removing the path to the file and just leave the file name in the content:

    header('Content-Type: application/pdf');
    header('Content-disposition: attachment; filename=ECM_IT_ResumeDownload.pdf');
    
    0 讨论(0)
提交回复
热议问题