470,810 Members | 1,142 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

downloading exe file does not work

Hi,
I have a link in my page that allows users to download an exe file.
However, when I download and run it, it briefly displays the DOS box and
nothing happens.
In my php file, I have:

header("Content-Type: application/octet-stream");
header("Content-Disposition: atachment; filename=$filename");
header("Content-Length: ".filesize("$path/$filename"));
header("Pragma: no-cache");
header("Expires: 0");
$fp=fopen("$path/$filename","r");
print fread($fp,filesize("$path/$filename"));
fclose($fp);
exit();

Am I missing something?

Also, if I click on the download again, after downloading it already, it
displays garbled text on the screen.

Thanks a lot for your help.
Jul 16 '05 #1
7 9842
"Xerxes" <as*******@hotmail.com> wrote:
I have a link in my page that allows users to download an exe file.
However, when I download and run it, it briefly displays the DOS box and
nothing happens. header("Content-Disposition: atachment; filename=$filename");


That should be:
header("Content-Disposition: attachment; filename=$filename");

JOn
Jul 16 '05 #2
"Jon Kraft" <jo*@jonux.co.uk> wrote in message
news:Xn**************************@130.133.1.4...
"Xerxes" <as*******@hotmail.com> wrote:
I have a link in my page that allows users to download an exe file.
However, when I download and run it, it briefly displays the DOS box and nothing happens.

header("Content-Disposition: atachment; filename=$filename");


That should be:
header("Content-Disposition: attachment; filename=$filename");

JOn


Thanks, but it still did not work.
I tried to zip the file and use content-type as application/x-zip.
Again, it downloads as a zip file but when I try to open it, I get the
following error message:

WinZip
Cannot open file: it does not appear to be valid archive.
If you downloaded this file, try downloading it again.

Thanks again.
Jul 16 '05 #3
How would I do this using ftp (maybe ftp_get(...))? I don't know what
the processes would be before issuing the ftp_get().
I think maybe ftp'ing the file would make a difference.

"Jon Kraft" <jo*@jonux.co.uk> wrote in message
news:Xn**************************@130.133.1.4...
"Xerxes" <as*******@hotmail.com> wrote:
"Jon Kraft" <jo*@jonux.co.uk> wrote:
"Xerxes" <as*******@hotmail.com> wrote:

> I have a link in my page that allows users to download an exe file. > However, when I download and run it, it briefly displays the DOS
box and
> nothing happens.

> header("Content-Disposition: atachment; filename=$filename");

That should be:
header("Content-Disposition: attachment; filename=$filename");


Thanks, but it still did not work.
I tried to zip the file and use content-type as application/x-zip.
Again, it downloads as a zip file but when I try to open it, I get the following error message:


Try:

header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=$filename");

Or, for a zip file:

header("Content-type: application/x-zip-compressed");
header("Content-Disposition: attachment; filename=$filename");

HTH;
JOn

Jul 16 '05 #4
I suppose you can do the print( fread()) statement, but it doesn't sound
right to me. I am always uncomfortable with using the return value of a
read statement without validation. You are taking the return value of
your fread() which SHOULD be the entire contents of the file and passing
that on as an input value to your print statement. You aren't validating
the results of your fread(). Also, and this is just my opinion, I don't
think you should be using using print() to output binary values.

If you think about it, you are abusing PHP a lot. You are reading a file
of some size - probably a BIG one into php memory and then printing it
back out - again without validation. Why bother?

This is what fpassthru() is for: It will output to the stream, close the
file for you and let you know if there is a problem. i.e.. give you
validation!

To test your results. Don't try to open the file.. Do a Diff between the
source file and the downloaded file. The header information won't be in
the file.

I assume that you have some reason that you don't want to make the file
available as a simple href? I guess doing a force-download is cool, but
is that the only reason?

dan
Jul 16 '05 #5
"Daniel Doman" <dd****@panix.com> wrote in message
news:Xn****************************@166.84.1.69...
I suppose you can do the print( fread()) statement, but it doesn't sound right to me. I am always uncomfortable with using the return value of a read statement without validation. You are taking the return value of
your fread() which SHOULD be the entire contents of the file and passing that on as an input value to your print statement. You aren't validating the results of your fread(). Also, and this is just my opinion, I don't think you should be using using print() to output binary values.

If you think about it, you are abusing PHP a lot. You are reading a file of some size - probably a BIG one into php memory and then printing it back out - again without validation. Why bother?

This is what fpassthru() is for: It will output to the stream, close the file for you and let you know if there is a problem. i.e.. give you
validation!

To test your results. Don't try to open the file.. Do a Diff between the source file and the downloaded file. The header information won't be in the file.

I assume that you have some reason that you don't want to make the file available as a simple href? I guess doing a force-download is cool, but is that the only reason?


Hi Dan,
thanks for the info. I am new to PHP and am still learning the ropes.
For this file download, users go to page where they supply their email
address and then are redirected to the download page.

Being new to PHP, I was wondering if you could give me an example of
file downloading using fpassthru().
I was thinking of ftp'ing the file but am not sure how to do that
either!! (I get as far as connecting to our ftp server, but the file
can't be found ("No such file or directory" error). I used ftp_get())

I appreciate your input and assistance.
Jul 16 '05 #6
Well I wouldn't suggest FTP'ing the file unless you want to add to your
headaches by actually setting up an FTP server.. This isn't PHP - its
really just html

You can set up an href that will download

as <A href="http://12.12.12.100/something/xyz.file">Give me some XYZ!</A>

if you really want to force feed your download, use the headers that you
had before, but test the fopen - thats just good programming e.g..
I am not changing your headers, I assume they are appropriate for
your file. I would have to check my notes to see if content length is
required.

Note that I am also testing for open on fopen. You should actually test
to result on fpassthru which will return false if something goes wrong,
but lets not get carried away here :)

//
//
$fp=fopen("$path/$filename","r");
if($fp) {
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Length: ".filesize("$path/$filename"));
header("Pragma: no-cache");
header("Expires: 0");
fpassthru($fp);
// fpassthru just spits out to stdout and
// closes the handle for you
}
else {
print("Dang me! I couldn't open $filename I feel Soooo sad!<br>");
}
I can't be sure. I am learning PHP too, but I rather doubt that print()
will handle a binary stream properly and in either case, it makes no
sense to read the file - especially if it is big - into a buffer just to
turn around and write it back out again. If there were no fpassthru()
function, it would make sense to write one using a fread/fwrite() loop.
But this type of thing is exactly what fpassthru was added to PHP for....

Jul 16 '05 #7
Xerxes wrote:
Hi,
I have a link in my page that allows users to download an exe file.
However, when I download and run it, it briefly displays the DOS box and
nothing happens.
In my php file, I have:

header("Content-Type: application/octet-stream");
header("Content-Disposition: atachment; filename=$filename");
header("Content-Length: ".filesize("$path/$filename"));
header("Pragma: no-cache");
header("Expires: 0");
$fp=fopen("$path/$filename","r");
print fread($fp,filesize("$path/$filename"));
fclose($fp);
exit();

Am I missing something?

Also, if I click on the download again, after downloading it already, it
displays garbled text on the screen.

Thanks a lot for your help.

I just noticed -- should you be opening the file "rb" instead of "r" ?

Jul 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jeff Cooper | last post: by
1 post views Thread by DBC User | last post: by
35 posts views Thread by keerthyragavendran | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.