Trouble with Mozilla and my PHP download counter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Willsher

    #1

    Trouble with Mozilla and my PHP download counter

    Hi all,

    PHP 4.2.2 on RedHat9

    I have some files on my website for people to download, and I want to
    generate the file location "on the fly", so the URL is held in a database.

    I have a simple PHP script, pasted below. IE users are fine, but Mozilla
    users actually see the file contents on screen, as garbage, instead of
    being presented with the "save as" dialog.

    The files for download have the .msi extension (binaries).

    Can anyone see what I'm doing wrong? Do I need to send different/additional
    headers?

    Many thanks!



    Jim

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

    <?php
    // Retrieve the parameters
    $FileID = isset($_REQUEST["ID"]) ? $_REQUEST["ID"] : -1;

    // Connect to the database
    $link = mysql_connect(" localhost", root", "xxxxxx") or die("Could not
    connect : " . mysql_error());
    mysql_select_db ("Downloads" ) or die("Could not select Downloads database");

    // Is this a valid download?
    $query = "SELECT * FROM DownloadItems WHERE FileID='$FileID '";
    $result = mysql_query($qu ery) or die("Query failed : " . mysql_error());
    $num_rows = mysql_num_rows( $result);

    if ($num_rows != 1)
    die("Invalid Download");

    // Obtain the row
    $row = mysql_fetch_arr ay($result, MYSQL_ASSOC);

    // Obtain the URL
    $FileURL = $row["FileURL"];

    // Release the database connection
    mysql_free_resu lt($result);

    // Redirect
    header("Content-Type: application/force-download");
    header("Locatio n: $FileURL");
    ?>



    Jim Willsher

    Homepages at http://www.jimwillsher.co.uk
  • Pedro Graca

    #2
    Re: Trouble with Mozilla and my PHP download counter

    Jim Willsher wrote:[color=blue]
    > <?php[/color]
    (snip)[color=blue]
    > // Redirect
    > header("Content-Type: application/force-download");
    > header("Locatio n: $FileURL");[/color]
    No, no! Do not redirect for a new request; use the same request!

    Instead of the redirection header, just output the file:

    readfile($FileU RL);
    [color=blue]
    > ?>[/color]


    Happy Coding :)
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Jim Willsher

      #3
      Re: Trouble with Mozilla and my PHP download counter

      Hi Pedro,

      But now the file just gets displayed in Internet Explorer too! This makes
      it worse!

      Go to http://www.jwillsher.co.uk/Includes/Download2.php?ID=1 to see what I
      mean. This is a temporary URL. The normal URL (live on the website) is


      The webpage itself is at






      Jim



      On 22 Feb 2004 16:58:27 GMT, Pedro Graca <hexkid@hotpop. com> wrote:
      [color=blue]
      >Jim Willsher wrote:[color=green]
      >> <?php[/color]
      >(snip)[color=green]
      >> // Redirect
      >> header("Content-Type: application/force-download");
      >> header("Locatio n: $FileURL");[/color]
      >No, no! Do not redirect for a new request; use the same request!
      >
      >Instead of the redirection header, just output the file:
      >
      > readfile($FileU RL);
      >[color=green]
      >> ?>[/color]
      >
      >
      >Happy Coding :)[/color]



      Jim Willsher

      Homepages at http://www.jimwillsher.co.uk

      Comment

      • Pedro Graca

        #4
        Re: Trouble with Mozilla and my PHP download counter

        Jim Willsher top posted (corrected):[color=blue]
        > On 22 Feb 2004 16:58:27 GMT, Pedro Graca <hexkid@hotpop. com> wrote:
        >[color=green]
        >>Jim Willsher wrote:[color=darkred]
        >>> <?php[/color]
        >>(snip)[color=darkred]
        >>> // Redirect
        >>> header("Content-Type: application/force-download");
        >>> header("Locatio n: $FileURL");[/color]
        >>No, no! Do not redirect for a new request; use the same request!
        >>
        >>Instead of the redirection header, just output the file:
        >>
        >> readfile($FileU RL);
        >>[color=darkred]
        >>> ?>[/color][/color]
        > But now the file just gets displayed in Internet Explorer too! This makes
        > it worse!
        >
        > Go to http://www.jwillsher.co.uk/Includes/Download2.php?ID=1 to see what I
        > mean. This is a temporary URL. The normal URL (live on the website) is
        > http://www.jwillsher.co.uk/Includes/Download.php?ID=1
        >
        > The webpage itself is at
        >
        > http://www.jwillsher.co.uk/Site/Software/BRU_Intro.php[/color]

        Please do not top post.

        You might want to try different headers.

        <?php
        // do checks

        header('Content-Type: application/binary');
        header('Content-Disposition: attachment; filename="' . basename($FileU RL) . '"');
        readfile($FileU RL);
        ?>
        --
        --= my mail box only accepts =--
        --= Content-Type: text/plain =--
        --= Size below 10001 bytes =--

        Comment

        • Jim Willsher

          #5
          Re: Trouble with Mozilla and my PHP download counter

          Hi Pedro,
          [color=blue]
          >Please do not top post.[/color]

          Given that I've been posted to NNTP groups for about 10 years, you're the
          first person to ever say that. Perhaps it's different in this NG, but in
          all my other groups it's the norm to top-post.
          [color=blue]
          >You might want to try different headers.
          >
          ><?php
          >// do checks
          >
          >header('Conten t-Type: application/binary');
          >header('Conten t-Disposition: attachment; filename="' . basename($FileU RL) . '"');
          >readfile($File URL);
          >?>[/color]

          Thanks, I'll give that a try. It looks okay in IE, so I'lll get somebody
          with Mozilla to try.

          Many thanks,



          Jim



          Jim Willsher

          Homepages at http://www.jimwillsher.co.uk

          Comment

          • Pedro Graca

            #6
            Re: Trouble with Mozilla and my PHP download counter

            Jim Willsher wrote:[color=blue]
            > Hi Pedro,
            >[color=green]
            >>Please do not top post.[/color]
            >
            > Given that I've been posted to NNTP groups for about 10 years, you're the
            > first person to ever say that. Perhaps it's different in this NG, but in
            > all my other groups it's the norm to top-post.[/color]

            Lots and lots of people top post here too. I just like it better when
            they don't :)

            I guess you were unlucky to have me in a nitpick mood. Don't change your
            habits because of what I say (but thank you for posting the way I like);
            I stopped long ago to give a negative score to top-posters, though they
            still get a low positive score in my newsreader :)
            [color=blue]
            > Thanks, I'll give that a try. It looks okay in IE, so I'lll get somebody
            > with Mozilla to try.[/color]

            Works with Mozilla too (I use Firebird).
            [color=blue]
            > Many thanks,[/color]

            You're very welcome.
            --
            --= my mail box only accepts =--
            --= Content-Type: text/plain =--
            --= Size below 10001 bytes =--

            Comment

            • Pedro Graca

              #7
              Re: Trouble with Mozilla and my PHP download counter

              Pedro Graca wrote:[color=blue]
              > Works with Mozilla too (I use Firebird).[/color]
              That's on Windows;

              also works with Elinks on Linux -- but with a small glitch: the name of
              the download is "Download.p hp" and not "whatever.m si".
              --
              --= my mail box only accepts =--
              --= Content-Type: text/plain =--
              --= Size below 10001 bytes =--

              Comment

              • Jim Willsher

                #8
                Re: Trouble with Mozilla and my PHP download counter

                On 22 Feb 2004 20:19:47 GMT, Pedro Graca <hexkid@hotpop. com> wrote:
                [color=blue]
                >Pedro Graca wrote:[color=green]
                >> Works with Mozilla too (I use Firebird).[/color]
                >That's on Windows;
                >
                >also works with Elinks on Linux -- but with a small glitch: the name of
                >the download is "Download.p hp" and not "whatever.m si".[/color]

                Hi Pedro,

                Great, excellent news! Many thanks for testing it all out!

                Any ideas what would cause the Elinks problem? My knowledge of http headers
                is very sketchy, so I don't know if I'm missing anything obvious.

                What I'm now doing is:

                header('Content-Type: application/binary');
                header("Locatio n: $FileURL");

                I tried the readfile() thing but it seemed to add a delay to the download.

                Thanks again!



                Jim

                PS I use Agent Newsreader on Windows. Any idea how I'd change it to stop
                top-posting? It's not something I had ever considered before, but I'd like
                to know how to change it. I can't see an option anywhere!




                Jim Willsher

                Homepages at http://www.jimwillsher.co.uk

                Comment

                • Pedro Graca

                  #9
                  Re: Trouble with Mozilla and my PHP download counter

                  Jim Willsher wrote:[color=blue]
                  > On 22 Feb 2004 20:19:47 GMT, Pedro Graca <hexkid@hotpop. com> wrote:
                  >[color=green]
                  >>Pedro Graca wrote:[color=darkred]
                  >>> Works with Mozilla too (I use Firebird).[/color]
                  >>That's on Windows;
                  >>
                  >>also works with Elinks on Linux -- but with a small glitch: the name of
                  >>the download is "Download.p hp" and not "whatever.m si".[/color][/color]
                  [color=blue]
                  > Great, excellent news! Many thanks for testing it all out!
                  >
                  > Any ideas what would cause the Elinks problem? My knowledge of http headers
                  > is very sketchy, so I don't know if I'm missing anything obvious.[/color]

                  No idea. But I don't think any Elinks user will want to download a .msi
                  file :) (and even if they do, they'll think having to type the filename
                  is ok).
                  [color=blue]
                  > What I'm now doing is:
                  >
                  > header('Content-Type: application/binary');
                  > header("Locatio n: $FileURL");[/color]

                  It does *NOT* work now!

                  You're sending the "Content-Type" header for a page, then redirecting to
                  another page which will *NOT* have the "Content-Type" and
                  "Content-Disposition" headers.

                  In Mozilla the URL http://www.jwillsher.co.uk/Includes/Download2.php?ID=1
                  asks where to save the file, but the other URLs you provided earlier
                  just display the file in the browser.
                  [color=blue]
                  > I tried the readfile() thing but it seemed to add a delay to the download.[/color]

                  Strange. The server has to open the file anyway, either with or without
                  the redirect. Anyway, I think you're stuck with using it (or a similar
                  function)


                  [color=blue]
                  > PS I use Agent Newsreader on Windows. Any idea how I'd change it to stop
                  > top-posting? It's not something I had ever considered before, but I'd like
                  > to know how to change it. I can't see an option anywhere![/color]

                  As far as I know (I used Agent before switching [mainly] to Linux) there
                  is no option for that. Agent puts the cursor at the top of the text,
                  which is a good place to start editing the previous post by removing
                  unnecessary lines and adding your own comments.
                  If you want to write first and edit after that, press Ctrl+End to go to
                  the end.
                  --
                  --= my mail box only accepts =--
                  --= Content-Type: text/plain =--
                  --= Size below 10001 bytes =--

                  Comment

                  • Stephen Poley

                    #10
                    Re: Trouble with Mozilla and my PHP download counter

                    On Sun, 22 Feb 2004 22:10:14 +0000, Jim Willsher <jim@nospam.co. uk>
                    wrote:
                    [color=blue]
                    >PS I use Agent Newsreader on Windows. Any idea how I'd change it to stop
                    >top-posting? It's not something I had ever considered before, but I'd like
                    >to know how to change it. I can't see an option anywhere![/color]

                    Just go to the bottom of page with Ctrl+End or Page-Down and start
                    typing. Though one would usually start by editing out extraneous quoted
                    material, of course.

                    HTH

                    --
                    Stephen Poley

                    Comment

                    • Jim Willsher

                      #11
                      Re: Trouble with Mozilla and my PHP download counter

                      On 22 Feb 2004 23:47:32 GMT, Pedro Graca <hexkid@hotpop. com> wrote:
                      [color=blue]
                      >Jim Willsher wrote:[color=green]
                      >> On 22 Feb 2004 20:19:47 GMT, Pedro Graca <hexkid@hotpop. com> wrote:
                      >>[color=darkred]
                      >>>Pedro Graca wrote:
                      >>>> Works with Mozilla too (I use Firebird).
                      >>>That's on Windows;
                      >>>
                      >>>also works with Elinks on Linux -- but with a small glitch: the name of
                      >>>the download is "Download.p hp" and not "whatever.m si".[/color][/color]
                      >[color=green]
                      >> Great, excellent news! Many thanks for testing it all out!
                      >>
                      >> Any ideas what would cause the Elinks problem? My knowledge of http headers
                      >> is very sketchy, so I don't know if I'm missing anything obvious.[/color]
                      >
                      >No idea. But I don't think any Elinks user will want to download a .msi
                      >file :) (and even if they do, they'll think having to type the filename
                      >is ok).
                      >[color=green]
                      >> What I'm now doing is:
                      >>
                      >> header('Content-Type: application/binary');
                      >> header("Locatio n: $FileURL");[/color]
                      >
                      >It does *NOT* work now!
                      >
                      >You're sending the "Content-Type" header for a page, then redirecting to
                      >another page which will *NOT* have the "Content-Type" and
                      >"Content-Disposition" headers.
                      >
                      >In Mozilla the URL http://www.jwillsher.co.uk/Includes/Download2.php?ID=1
                      >asks where to save the file, but the other URLs you provided earlier
                      >just display the file in the browser.
                      >[color=green]
                      >> I tried the readfile() thing but it seemed to add a delay to the download.[/color]
                      >
                      >Strange. The server has to open the file anyway, either with or without
                      >the redirect. Anyway, I think you're stuck with using it (or a similar
                      >function)
                      > http://www.php.net/readfile
                      >
                      >[color=green]
                      >> PS I use Agent Newsreader on Windows. Any idea how I'd change it to stop
                      >> top-posting? It's not something I had ever considered before, but I'd like
                      >> to know how to change it. I can't see an option anywhere![/color]
                      >
                      >As far as I know (I used Agent before switching [mainly] to Linux) there
                      >is no option for that. Agent puts the cursor at the top of the text,
                      >which is a good place to start editing the previous post by removing
                      >unnecessary lines and adding your own comments.
                      >If you want to write first and edit after that, press Ctrl+End to go to
                      >the end.[/color]

                      Okay, take two! Could you try this one instead? Normal URL:



                      You'll notice there's about 10 seconds delay from the "website found" to
                      the save-as dialog appearing. This only happens when I use the readfile()
                      command.


                      Jim



                      Jim Willsher

                      Homepages at http://www.jimwillsher.co.uk

                      Comment

                      • Pedro Graca

                        #12
                        Re: Trouble with Mozilla and my PHP download counter

                        Jim Willsher wrote:[color=blue]
                        > Okay, take two! Could you try this one instead? Normal URL:
                        >
                        > http://www.jwillsher.co.uk/Includes/Download.php?ID=1
                        >
                        > You'll notice there's about 10 seconds delay from the "website found" to
                        > the save-as dialog appearing. This only happens when I use the readfile()
                        > command.[/color]

                        Nope. Immediately starts showing 'funny' characters in the browser (be
                        it Mozilla or Elinks).
                        --
                        --= my mail box only accepts =--
                        --= Content-Type: text/plain =--
                        --= Size below 10001 bytes =--

                        Comment

                        Working...