Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 442
  • Last Modified:

Attachment download works in FF, Safari & Chrome but empty file in IE

I'm using a simple php script to download files from a server directory, similar to an email attachment download. The code works fine in Chrome, Firefox & Safari, but in IE (IE7 I think) the file shows up as empty. Typical files are Word docs or pdf; all come up as empty.

Code:

    $file = $_GET["download_file"];
    $path = "/home/xxx/yyy/";
    $fullfile = $path . $file;
    $size = $_GET["size"];
    $type = $_GET["type"];

    if (file_exists($fullfile)) {
      header('Content-type: "' . $type . '"');
      header('Content-disposition: attachment; filename="' . $file . '"');
      header("Content-transfer-encoding: Binary");
      header('Content-length: "' . $size . '"');
      readfile("$fullfile");
    } else
      echo "No file selected or file not found: $fullfile";

Are there additional headers that IE requires?

0
jimdgar2
Asked:
jimdgar2
  • 4
  • 3
  • 2
1 Solution
 
Ray PaseurCommented:
Try this script and let me know if it works for you.  You can modify it to use the GET arguments instead of the hardcoded file name.
<?php // RAY_force_download.php
error_reporting(E_ALL);



// A FILE TO DOWNLOAD - THIS LINK COULD COME IN THE URL VIA $_GET, OR COULD BE GENERATED INSIDE THE SCRIPT
$url = "http://www.google.com/intl/en_ALL/images/logo.gif";

// USE CASE
force_download($url);




// FUNCTION TO FORCE A DOWNLOAD FROM A FILE
function force_download($filename)
{
    // GET THE CONTENTS OF THE FILE
    $filedata = @file_get_contents($filename);

    if ($filedata)
    {
        // GET A NAME FOR THE FILE
        $basename = basename($filename);

        // THESE HEADERS ARE USED ON ALL BROWSERS
        header("Content-Type: application-x/force-download");
        header("Content-Disposition: attachment; filename=\"$basename\"");
        header("Content-length: ".(string)(strlen($filedata)));
        header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

        // THIS HEADER MUST BE OMITTED FOR IE 6+
        if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE '))
        {
            header("Cache-Control: no-cache, must-revalidate");
        }

        // THIS IS THE LAST HEADER
        header("Pragma: no-cache");

        // FLUSH THE HEADERS TO THE BROWSER
        flush();

        // CAPTURE THE FILE IN THE OUTPUT BUFFERS - WILL BE FLUSHED AT SCRIPT END
        ob_start();
        echo $filedata;
    }

    // ERROR
    else
    {
        die("ERROR: UNABLE TO OPEN $filename");
    }
}

Open in new window

0
 
jimdgar2Author Commented:
Ray:

No go. I first tried patching in pieces of your code which were missing in mine. When this didn't work I used your code in its entirety (adding my GETS). Still has the same result.
0
 
Ray PaseurCommented:
Please give us a test data set.  I will try to illustrate the principles for you.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Dave BaldwinFixer of ProblemsCommented:
A bunch of pages I looked including a couple on Microsoft say that the values should not be quoted.  Although the PHP.net page shows the filename in quotes for one example.  This is from a Microsoft page but "application/download" isn't listed in the 'official' list at http://www.iana.org/assignments/media-types/index.html :

Content-Type = application/download
Content-Disposition = attachment; filename=test.txt
0
 
jimdgar2Author Commented:
Dave:

Bingo! Removing the quotes fixed the problem. Dang I love Microsoft.

Thank you
0
 
Dave BaldwinFixer of ProblemsCommented:
You're welcome.  You know what they say, the nice thing about standards is that there are so many to choose from...
0
 
Ray PaseurCommented:
Can you please verify whether this was IE7 or another version, and please post the script that you got to work, thanks.
0
 
jimdgar2Author Commented:
Yes, it's IE 7.0

Here's my final script. I have verified it on IE7, Chrome 10.0, Firefox 3.5.9 and Safari 5.0.5.

<?php

    $file = $_GET["download_file"];
    $path = "/home/xxx/yyy/";
    $fullfile = $path . $file;
    $size = $_GET["size"];
    $type = $_GET["type"];
    if (file_exists($fullfile)) {
       header('Content-transfer-encoding: Binary');
       header('Content-length: ' . $size);
       header('Content-type: ' . $type);
       header('Content-disposition: attachment; filename=' . $file);
       readfile("$fullfile");
    } else {
       echo "No file selected or file not found: $fullfile";
    }

?>
0
 
Ray PaseurCommented:
Thanks!  All the best, ~Ray
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now