470,870 Members | 1,447 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

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($query) 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_array($result, MYSQL_ASSOC);

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

// Release the database connection
mysql_free_result($result);

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

Jim Willsher

Homepages at http://www.jimwillsher.co.uk
Jul 17 '05 #1
11 2247
Jim Willsher wrote:
<?php (snip) // Redirect
header("Content-Type: application/force-download");
header("Location: $FileURL"); No, no! Do not redirect for a new request; use the same request!

Instead of the redirection header, just output the file:

readfile($FileURL);
?>

Happy Coding :)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2
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
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


Jim

On 22 Feb 2004 16:58:27 GMT, Pedro Graca <he****@hotpop.com> wrote:
Jim Willsher wrote:
<?php

(snip)
// Redirect
header("Content-Type: application/force-download");
header("Location: $FileURL");

No, no! Do not redirect for a new request; use the same request!

Instead of the redirection header, just output the file:

readfile($FileURL);
?>

Happy Coding :)


Jim Willsher

Homepages at http://www.jimwillsher.co.uk
Jul 17 '05 #3
Jim Willsher top posted (corrected):
On 22 Feb 2004 16:58:27 GMT, Pedro Graca <he****@hotpop.com> wrote:
Jim Willsher wrote:
<?php

(snip)
// Redirect
header("Content-Type: application/force-download");
header("Location: $FileURL");

No, no! Do not redirect for a new request; use the same request!

Instead of the redirection header, just output the file:

readfile($FileURL);
?>

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


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($FileURL) . '"');
readfile($FileURL);
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #4
Hi Pedro,
Please do not top post.
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.
You might want to try different headers.

<?php
// do checks

header('Content-Type: application/binary');
header('Content-Disposition: attachment; filename="' . basename($FileURL) . '"');
readfile($FileURL);
?>


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
Jul 17 '05 #5
Jim Willsher wrote:
Hi Pedro,
Please do not top post.
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.


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 :)
Thanks, I'll give that a try. It looks okay in IE, so I'lll get somebody
with Mozilla to try.
Works with Mozilla too (I use Firebird).
Many thanks,


You're very welcome.
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #6
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.php" and not "whatever.msi".
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #7
On 22 Feb 2004 20:19:47 GMT, Pedro Graca <he****@hotpop.com> wrote:
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.php" and not "whatever.msi".


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("Location: $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
Jul 17 '05 #8
Jim Willsher wrote:
On 22 Feb 2004 20:19:47 GMT, Pedro Graca <he****@hotpop.com> wrote:
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.php" and not "whatever.msi".

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.
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).
What I'm now doing is:

header('Content-Type: application/binary');
header("Location: $FileURL");
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.
I tried the readfile() thing but it seemed to add a delay to the download.
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

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!


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 =--
Jul 17 '05 #9
On Sun, 22 Feb 2004 22:10:14 +0000, Jim Willsher <ji*@nospam.co.uk>
wrote:
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!


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
Jul 17 '05 #10
On 22 Feb 2004 23:47:32 GMT, Pedro Graca <he****@hotpop.com> wrote:
Jim Willsher wrote:
On 22 Feb 2004 20:19:47 GMT, Pedro Graca <he****@hotpop.com> wrote:
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.php" and not "whatever.msi".

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.


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).
What I'm now doing is:

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


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.
I tried the readfile() thing but it seemed to add a delay to the download.


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

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!


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.


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.
Jim

Jim Willsher

Homepages at http://www.jimwillsher.co.uk
Jul 17 '05 #11
Jim Willsher wrote:
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.


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 =--
Jul 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Fernando Rodríguez | last post: by
17 posts views Thread by George Hester | last post: by
reply views Thread by Marius III | last post: by
5 posts views Thread by Mickey | last post: by
7 posts views Thread by mistral | last post: by
9 posts views Thread by johnd126 | last post: by
12 posts views Thread by devospice | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.