473,320 Members | 1,612 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 10114
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ASP Spam Fighter | last post by:
Hello all, I don't know how to get around this one... If anybody can help me with this problem, I would appreciate it very much. I've been trying to send a (large) file to the browser via a...
5
by: kevin | last post by:
Hi, Any help with this would be really appreciated! I'm trying to download a file from a remote server. The access permissions is okay but the problem I'm facing is that the file is getting...
4
by: Jeff Cooper | last post by:
There must be something I'm missing. No else else seems to have this issue -- at least I don't see it mentioned anywhere. (Also, my appologies for my earlier posts in which I mistakenly referred...
7
by: theyas | last post by:
How can I get my code to NOT display two "Open/Save/Cancel/More Info" dialog boxes when using the "Response.WriteFile" method to download a file to IE I've asked about this before and didn't get a...
4
by: Edwin Knoppert | last post by:
I have a problem with downloading a PDF by using the response object. A part of it: ..Response.Clear() ..Response.ContentType = sFileType ..Response.AppendHeader("Content-Disposition",...
5
by: fniles | last post by:
We created an ActiveX control and marked it as safe for scripting using Implements IObjectSafety. We then created a CAB file and signed it using Verisign. We also created a license file (LPK file)...
1
by: DBC User | last post by:
Hello, I would like to copy a very large file (80MB) from the web, which is the best approach for downloading such a large file and also how can I show how much percent completed to user. I...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.