SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member ssmatthew's Avatar
    Join Date
    May 2004
    Location
    Sydney
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Difficulties downloading PDF files in IE (via PHP script)

    [FONT=Times New Roman]I am having great difficulties with downloading PDF files in IE. The files are sitting outside the web root (for security purposes) and are called via a PHP script.

    The difficulties are as follows:
    (1) Inline content doesn't seem to work, only attachement (All PC IE)
    (2) Loading the same file a second time generates a "File not found" error in Acrobat. (Windows 2000 onwards, IE 6)

    The (partial) script in PHP is as follows:
    $fname = "client_pdf.pdf"; // also tried basename($file_path);
    $sizee = filesize($file_path);
    header("Content-Type: application/pdf; name=\"".$fname."\"");
    header("Content-Transfer-Encoding: binary");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s", filemtime($img)) . " GMT\n");
    header("Accept-Ranges: bytes\n");
    header("Content-Length: ".$sizee."");
    header("Content-Disposition: attachment; filename=\"".$fname."\"");
    header("Expires: 0");
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: public");
    readfile($file_path);

    I've set the script into a file called "load_pdf.pdf" and changed the .htaccess file to parse .pdf files as PHP scripts (this is the only way that PDF files get called on the server so there is no parsing conflict there!)

    Any suggestions? Thanks in anticipation for your assistance.

  2. #2
    SitePoint Evangelist ClevaTreva's Avatar
    Join Date
    Jan 2004
    Location
    Chipping Campden, UK
    Posts
    403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    I had a similar problem recently. Did what you did by changing .htaccess

    However, Apache ignores that if another setting in the httpd.conf file is off, which it was. The host wasn't prepared to allow it on, as the server cpu load increases by unnecessarily pushing the file through the parser.

    I gave up at that point and went back to the drawing board. I can't remember for sure how I fixed it, but it was something to do with the header settings.

    It was a pain to find because there was so little on the web about it.

    Anyway, this is a blog from the php site:

    I have found that for certain formats, for example, PDF, the simple code:
    header("Content-type: application/x-download");
    header("Content-Disposition: attachment; filename=$file_download_name;");
    @readfile($file_server_path);
    does not work, and produces corrupted files for all browsers on all platforms. During the downloads, I noticed that the browser was not receiving ahead of time the length of the file, which was somehow causing some formats to be corrupted.
    e.g.: "Downloading file example.pdf: 347.2 KB of ?"
    I solved the problem by adding more headers:
    $download_size = filesize($file_server_path);
    header("Content-type: application/x-download");
    header("Content-Disposition: attachment; filename=$file_download_name;");
    header("Accept-Ranges: bytes");
    header("Content-Length: $download_size");
    @readfile($file_server_path);
    Somehow the files aren't corrupted when the browser knows how big they are from the start.
    I hope that helps



    Trevor
    "Good artists copy, great artists steal."
    - Pablo Picasso
    The image of ClevaTreva is drawn by Rhys, and is a GOOD likeness

  3. #3
    SitePoint Member ssmatthew's Avatar
    Join Date
    May 2004
    Location
    Sydney
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I've looked at lots of headers, but just can't seem to get it to work. The size is in there...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •