Hi

I've got what seems to be the fairly standard problem of forcing the browser to download (save file-dialog) files of the types .txt, .doc, .xls, etc.

I've been browsing the forums for an answer but the ones provided didn't work for me. This is what I use:

<?PHP
if ($startdownload == "yes") {
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$dfilename");
header("Content-Transfer-Encoding: binary");
header("Location: http://kihlbom.com/afk/download/$dfilename");
exit;
}
?>

I've also tried using header("Content-type: application/x-ms-download"); as suggested by someone in this forum.

I really need this to work, so if you guys could help me I'd be grateful.

BTW, I'm using IE 5.0/MacOS 8.6 for testing, but I've also tested with IE 5.0/Win98.

Thanks,

CJ Kihlbom

    5 months later

    Hi CJ, I'm having the same problem; I want to download a .txt report to the user, not display it in their browser (and expect them to figure out how to do "Save As...").

    If you can find a site that does what we want it to do, I have a program to show us exactly what the headers are from the site to the browser; maybe that'll help.

    best,
    Eric

      17 days later

      source: http://www.rachnajit.com/lekhani/archive/Aug2000.html

      <%
      response.addHeader "content-disposition",
      "attachment; filename=helloworld.txt"
      %>
      then opening helloworld.txt and sending its contents to IE like:

      <%
      Dim fso,textstream, contents

      Set fso = CreateObject("Scripting.FileSystemObject")
      Set textstream = fso.OpenTextFile("helloworld.txt",1)
      contents = textstream.readall
      textstream.close

      Response.Write contents
      %>

        The following code does the trick :

        // Basic check, redirect if no valid parameter given
        if (($REQUEST_METHOD != "GET") or empty($name))
        {
        Header("Location: default.php");
        exit;
        }

        $size = filesize("/www/docs/$name");

        //header("Content-Type: application/ms-x-download");
        header("Content-Type: application/octet-stream");
        header("Content-Length: $size");
        header("Content-Disposition: attachment; filename=$name");
        header("Content-Transfer-Encoding: binary");

        $fh = fopen("/www/docs/$name", "r");
        fpassthru($fh);
        exit;

        I ran into another IE bug using this script though. I was working on a members section for a website, using session variables for logins. When I added the call to session_start(); at the top of this script, the script would no longer work on IE 5.0 build 2920 or later ... It always said it couldn't connect to the server.

        Best regards,

        Wim

          23 days later

          Hello,
          I have tried this, and it works good, but when the file is done downloading, the browser still continues to do something. Below is the exact code I am using. Please see if there is something wrong. Thank you.

          if($type == "word")
          {
          $size = filesize("resume.doc");
          header("Content-Type: application/octet-stream");
          header("Content-Length: $size");
          header("Content-Disposition: attachment; filename=resume.doc");
          header("Content-Transfer-Encoding: binary");
          $fh = fopen("resume.doc", "r");
          fpassthru($fh);
          record_download();
          exit;
          }

          // Used to count how many people download this file.
          function record_download()
          {
          $count = file("downloads.txt");
          $count = $count[0];
          settype($count, "integer");
          $count++;

              if($fd = @fopen("downloads.txt", "w"))
              {
                      fputs($fd, $count);
              }

          }

            3 months later

            All,

            I finally tracked down a solution to this bug with Internet Explorer 5.5 SP 1 for Windows. The fix isn't great, because is isn't a server issue, its a client issue.

            There is a bug in mshtml.dll versions older than 5.50.4612.600. This is documented in Microsoft Product Support article Q281119.

            The support download (Hotfix) than contains the update to mshtml.dll is Q286045. This Hotfix will update mshtml.dll to version 5.50.4613.1700 and will cause Internet Explorer to properly read the Content-Disposition header.

            Hope this help!

            Brandon M. Wise

              a month later

              I've used the code shown below...

              <?
              $size = filesize("file.txt");
              header("Content-Type: application/octet-stream");
              header("Content-Length: $size");
              header("Content-Disposition: attachment; filename=file.txt");
              header("Content-Transfer-Encoding: binary");
              $fh = fopen("file.txt", "r");
              fpassthru($fh);
              exit;

              Netscape downloads fine. But with IExplore, the only result is that the file contents displays in the browser. The file download dialog does not come up.

              Any suggestions?

                Here's a workaround that works for me. It detects what browser is used and modifies the "Content-Disposition"-header accordingly.

                <?php

                /
                getfile.php
                /

                if (($REQUEST_METHOD != "GET") or empty($name))
                {
                Header("Location: /");
                exit;
                }

                // Hack for IE-bug
                if (strstr($HTTP_USER_AGENT, "MSIE"))
                {
                $attachment = "";
                }
                else
                {
                $attachment = " attachment;";
                }

                $size = filesize("/full/path/to/downloads/$name");
                $fh = fopen("/full/path/to/downloads/$name", "r");

                header("Content-Type: application/x-ms-download");
                header("Content-Length: $size");
                header("Content-Disposition:$attachment filename=$name");
                header("Content-Transfer-Encoding: binary");

                fpassthru($fh);
                exit;

                ?>

                Hope this helps,

                Wim

                  Finally! A solution to the IE 5.5 issue. Your solution worked beautifully. Thanks.

                    Works great but,....

                    I tried using this with an Acrobat PDF, and the viewer opened in the browser. Any workarounds for this one?

                    BTW, it was in IE5.5

                      Here's what i did... works on IE4up and Netscape 4up.

                      Use a query for the name with no extension i.e.

                      getfile.php?filename


                      <?php
                      /
                      Disposition fix by Wim Vandersmissen
                      /
                      if (strstr($HTTP_USER_AGENT, "MSIE"))
                      {
                      $attachment = "";
                      }
                      else
                      {
                      $attachment = " attachment;";
                      }

                      /
                      download file
                      /
                      $fileName = "$QUERY_STRING.pdf";
                      $filesize = filesize($fileName);
                      if($filesize) {
                      Header("Content-Type: application/x-ms-download");
                      Header("Content-Length: ".$filesize);
                      Header("Content-Disposition: attachment; filename=".$fileName);
                      @readfile($fileName);
                      }

                      ?>

                      This still has a glitch. If you click open on the dialog box, a second box opens then you can click open and the PDF will open in the viewer. Post a fix if you have one.

                        [snip]

                        Header("Content-Disposition: attachment; filename=".$fileName);
                        @readfile($fileName);
                        }

                        ?>

                        This still has a glitch. If you click open on the dialog box, a second box opens then you can click open and the PDF will open in the viewer. Post a fix if you have one.

                        ===============

                        That was actually what this thread was all about :p
                        The Content-Disposition header should look like this, it will remove the annoying first popup in IE :

                        Header("Content-Disposition:$attachment filename=".$fileName);

                        Note the $attachment instead of ' attachment;', for some unknown reason IE 5.X shows extra popups if you add ' attachment ;' to the Content-Disposition header. If you leave it out however, IE 5.X works as it should (but then Netscape no longer works :p). That's why I tried to fix things by checking if the user is using an MSIE browser.

                        BTW Has anyone tested the hack on MSIE 4.X or 6.X? If so, does it work on these versions too, or should the hack only be applied to MSIE 5.X?

                          Oops. I goofed there sorry. The problem i am getting is this. The initial Dialog gives me ... "downloading file from Domain", and it also says "getfile.php from domain" not "file.pdf from domain"... do i make sense? So if i click 'open' on the first dialog it then goes to the second dialog that then says "file.pdf from domain". Then i can view the file. Though the download goes right into downloading the file. Netscape works just fine 4 and 6. The download works fine on IE6beta too.

                            Did IE4 give you the speficied filename in the save as dialog? I've been playing around with this and have it working for NS4.x and IE5.5 but with IE4 it doesn't seem to properly read the Content-Disposition header.

                            If I remove the header it uses the script as the filename without the extension. Leaving the line in (withor without the attachment portion) comes back with a random character string for the name.

                            Any thoughts on how to fix this?

                              a month later

                              Hi,

                              If any one know how to download mutiple files at one download process?

                              Jackson Tsai

                                24 days later

                                IE 5.5 bug, Yes, I know.

                                but what should I do for the idle people who do not update their browsers?

                                please, tell me the solution.

                                Brandon M. Wise wrote:

                                All,

                                I finally tracked down a solution to this bug with Internet Explorer 5.5 SP 1 for Windows. The fix isn't great, because is isn't a server issue, its a client issue.

                                There is a bug in mshtml.dll versions older than 5.50.4612.600. This is documented in Microsoft Product Support article Q281119.

                                The support download (Hotfix) than contains the update to mshtml.dll is Q286045. This Hotfix will update mshtml.dll to version 5.50.4613.1700 and will cause Internet Explorer to properly read the Content-Disposition header.

                                Hope this help!

                                Brandon M. Wise

                                  22 days later

                                  I used your script on IE5.5 but the page show me a plug-in icon, and IE ask me to download an active-x plug-in.
                                  Can you tell me why. The other scripts do well on IE5.5 if I open the page as a new browser page.
                                  Thank you.

                                    I`ve problems too with this f*** MSIE 5.5

                                    the poppup window savely come up, but it doesn`t show the real filename!

                                    the code looks like this:

                                    $file=mysql_fetch_array($result);
                                    $type = $file["type"];
                                    $size = $file["size"];
                                    $filename = $file["filename"];
                                    $data = $file["data"];

                                     header("Content-type:$type");
                                     header("Content-length:$size");
                                     header("Content-Disposition: attachment; filename=\"$filename\"");
                                     header("Content-Description:PHP Generated Data");
                                     header("Content-Transfer-Encoding: binary");

                                    with Netscape 6.1 it works fine 🙂

                                    knows anyone more? it would be greatfull

                                      17 days later

                                      Have you encountered the following problem:

                                      Using IE5.5 through an ISP dial-up connection, when I specify "Content-Length: $size" where $size is the file length, the transfer begins correctly but is terminated straight away, and I get a zero-length file. It works fine with Netscape 6, or with a direct LAN connection.

                                      By removing the "Content-Length" all is OK with IE (except no progress bar).

                                      I am running PHP4.0.6 on Linux / Apache.