dcsimg
www.webdeveloper.com
Results 1 to 11 of 11

Thread: How to force Download in IE...

  1. #1
    Join Date
    Mar 2004
    Posts
    9

    How to force Download in IE...

    Hi,

    Would somebody could tell me how to force download in IE? I saw someone did a code (as shown below) and I 've tested it works in Netscape but does not work in IE, so I added some changes on it, but still can;t work in IE! Could sone of you kindly to show me why and how to fix it!

    Thanks a lot!

    =========
    <?PHP
    ....
    if (strstr($HTTP_USER_AGENT,"MSIE")) {
    $attachment = " ";
    } else {
    $attachment = " attachment;";
    }
    $saveasname="extract.xls";
    //header("Content-Type: application/octet-stream");
    header('Content-Type: application/vnd.ms-excel');
    //header("Content-Disposition: attachment; filename=extraction.xls");
    //header('Content-Disposition: $attachment filename="extraction.xls";');
    header('Content-Disposition: $attachment filename="'.$saveasname.'"');
    header('Content-Transfer-Encoding: binary');
    header('Pragma: no-cache');
    header('Expires: 0');
    print "$header\n$data";
    ?>

  2. #2
    Join Date
    Mar 2003
    Location
    Timisoara, Romania
    Posts
    33
    <?php
    // downloading a file
    $filename = $HTTP_GET_VARS['path'];

    // fix for IE catching or PHP bug issue
    header("Pragma: public");
    header("Expires: 0"); // set expiration time
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    // browser must download file from server instead of cache

    // force download dialog
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");

    // use the Content-Disposition header to supply a recommended filename and
    // force the browser to display the save dialog.
    header("Content-Disposition: attachment; filename=".basename($filename).";");

    /*
    The Content-transfer-encoding header should be binary, since the file will be read
    directly from the disk and the raw bytes passed to the downloading computer.
    The Content-length header is useful to set for downloads. The browser will be able to
    show a progress meter as a file downloads. The content-lenght can be determines by
    filesize function returns the size of a file.
    */
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($filename));

    readfile("$filename");
    exit();

    ?>

  3. #3
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104

  4. #4
    Join Date
    Jul 2003
    Location
    UK
    Posts
    441
    damin it,

    i was going to post that.... thats the third time you done that pyro

  5. #5
    Join Date
    Mar 2003
    Location
    Timisoara, Romania
    Posts
    33
    but what, what have I post is not good? I bet you it works well for all browsers

  6. #6
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Originally posted by whiteadi
    but what, what have I post is not good? I bet you it works well for all browsers
    I was simply providing another example - I hadn't tried your code.

  7. #7
    Join Date
    Mar 2003
    Location
    Timisoara, Romania
    Posts
    33
    yeah I was just joking man

  8. #8
    Join Date
    Mar 2004
    Posts
    9

    Thumbs up

    Thank you very very very much!!!
    I fixed it!!!

  9. #9
    Join Date
    Nov 2006
    Posts
    39
    This is maybe an old topic, but i'm really really desperate in trying to get it work.
    Because i copy and pasted the code above exactly and it doesn't work

    I really tried about 20 'force download scripts' and none has worked so far...
    But i'm hopefull because at least with this script, i already get a download window, where i can choose if i want to save or open the file, but the problem is that php doesn't find the file, because the download window syas that the size of my jpeg is about 500 bytes, which is way too less of course!

    I think it could have something to do with permissions?? Because the php script is in a different directory then the actual picture.

    Can anybody help me please because i've really been looking for a solution for this force download thing for days and days if i add all the hours.....

    THANKS!!

  10. #10
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    typically, php runs as the apache user. this is usually "nobody"

    what are the permissions on the file/directory.

  11. #11
    Join Date
    Nov 2006
    Posts
    39
    thanks for your reply.
    all the permissions are set to 755, so i don't think that could be the problem.

    I have been digging into this problem the last few hours and this is what i discovered :

    appearantly the problem is that the 'readfile' function, doesn't have permission to read the pic that i want , 'open_basedir restriction in effect'

    I've been looking on the php.net site and i found that the problem might be that the openbase directory is put to the wrong value (i've checked this for my ISP and its value is something with 'temp', so definitely not the directory where my pictures are)

    So i think that's why it won't work
    The solution would be that i have all my pictures in the same directory as this php script, but unfortunately i can't do that.

    So i wrote an email to my ISP to ask if and how i can adjust this php.ini file (because appearently it depends from ISP to ISP), so that i can change the openbase dir.

    And then hope this solves my problem
    Last edited by m.i.c.; 12-17-2006 at 09:43 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles