We help IT Professionals succeed at work.

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

jimdgar2
jimdgar2 asked
on
Medium Priority
450 Views
Last Modified: 2012-05-11
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?

Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014

Commented:
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

Author

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.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Please give us a test data set.  I will try to illustrate the principles for you.
Fixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Dave:

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

Thank you
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
You're welcome.  You know what they say, the nice thing about standards is that there are so many to choose from...
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Can you please verify whether this was IE7 or another version, and please post the script that you got to work, thanks.

Author

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";
    }

?>
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Thanks!  All the best, ~Ray
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.