@lexi.ch home, code, stuff

PHP: Correct File Delivery Headers for Internet Explorer 15.06.2011

Internet explorer is somewhat silly when it comes to download a file from a non-file URL (like a script, e.g. index.php?file=myfile.txt). This little PHP code on the server-side solved all my IE problems:

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$orig_filename."\"");
header("Pragma: "); // IMPORTANT FOR IE
header("Cache-Control: "); // IMPORTANT FOR IE
$fd = fopen ($fullPathToDonwloadFile, "r");
while(!feof($fd)) {
    $buffer = fread($fd, 2048);
    echo $buffer;
}