Professional Web Applications Themes

downloading exe file does not work - PHP Development

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

  1. #1

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


    Xerxes Guest

  2. #2

    Default Re: downloading exe file does not work

    "Xerxes" <ashkaan57hotmail.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
    Jon Kraft Guest

  3. #3

    Default Re: downloading exe file does not work

    "Jon Kraft" <jonjonux.co.uk> wrote in message
    news:Xns93A7A9443949Cjonjonuxcouk130.133.1.4...
    > "Xerxes" <ashkaan57hotmail.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.


    Xerxes Guest

  4. #4

    Default Re: downloading exe file does not work

    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" <jonjonux.co.uk> wrote in message
    news:Xns93A7BEE38B317jonjonuxcouk130.133.1.4...
    > "Xerxes" <ashkaan57hotmail.com> wrote:
    >
    > > "Jon Kraft" <jonjonux.co.uk> wrote:
    > >> "Xerxes" <ashkaan57hotmail.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

    Xerxes Guest

  5. #5

    Default Re: downloading exe file does not work

    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
    Daniel Doman Guest

  6. #6

    Default Re: downloading exe file does not work

    "Daniel Doman" <ddomanpanix.com> wrote in message
    news:Xns93A8A201730B9ddomanpanixcom166.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.


    Xerxes Guest

  7. #7

    Default Re: downloading exe file does not work

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

    Daniel Doman Guest

  8. #8

    Default Re: downloading exe file does not work

    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" ?

    Joshua Ghiloni Guest

Similar Threads

  1. Problem with file downloading
    By Zam in forum ASP Components
    Replies: 0
    Last Post: June 13th, 11:35 PM
  2. Downloading file
    By Mikey in forum Macromedia Director Basics
    Replies: 1
    Last Post: April 27th, 03:29 AM
  3. Downloading file(s) from CD
    By gbt webforumsuser@macromedia.com in forum Macromedia Director Basics
    Replies: 1
    Last Post: December 25th, 09:31 PM
  4. Downloading a file from a CD
    By mpz111 webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 3
    Last Post: November 24th, 09:23 PM
  5. Downloading a file
    By Steven in forum ASP.NET General
    Replies: 5
    Last Post: November 14th, 05:28 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139