I am saving a text file onto the client's hard drive using fpassthru().
Finally got it to work fine after reading the many messages on this forum.
I prefer to have all of the code in a function, so that it can be used widely within the program without extra coding.
However there is one line that is messing things up; it is the content-dispositon line. If this is in the function, the download works alright, but the code is not returned to the calling program. What happens is that the browser shows the egg time as though it is still waiting for the page to load, and the user has to press the stop button to stop this. This is tiresome and unaesthetic.
I have solved the problem by having the Content-Disposition line in the calling page and not in the function, but the rest of the code in the function, and then the whole thing works just fine.
Anyone any ideas why I have to place it there. It looks clumsy not having it in the function.
Note that the function has to be high up the code, before any output, or the headers will not work. Here is the whole function code:-
function savefile($savestring, $tempfilename, $newfilename )
{
// Wim's hack for IE-bug
if (strstr($HTTP_USER_AGENT, "MSIE"))
{
$attachment = "";
}
else
{
$attachment = " attachment;";
}
$tempfilename = "d:/".$tempfilename;
$size = filesize($tempfilename);
header("Content-Type: application/x-ms-download");
header("Content-Length: $size");
// If this is here it does not return - so remmed out and put in calling code
//header("Content-Disposition:$attachment filename=$newfilename");
header("Content-Transfer-Encoding: binary");
// Open and download a tempt file with the info
$fh = fopen($tempfilename, "r");
fpassthru($fh);
fclose ($fh);
// Remove the temp file on the server
unlink($tempfilename);
$success = "success";
return $success;
//exit;
}