Hi all,
I read online about a way to "force" a file download dialog box using the Content-Disposition header combined with fpassthru() in PHP, like so:
(query string is to be specified as a filename - without path - to a file in the current working directory)
Code:
$filename = getenv('QUERY_STRING');
$stuff = preg_split( "/\./", $filename );
$ext = array_pop( $stuff );
$mime = array(
"html" => "text/html",
"htm" => "text/html",
"txt" => "text/html",
"rtf" => "application/rtf",
"doc" => "application/msword",
"zip" => "application/zip",
);
header( 'Content-Type: ' . $mime[ strtolower( $ext ) ] );
header( "Content-Disposition: attachment;filename=$filename" );
$h = fopen( $filename, 'r' );
fpassthru( $h );
fclose( $h );
exit();
It works fine, but for some reason whenever it passes a text file (.txt), all the newlines are removed after downloading it. doc/html/htm/zip files download flawlessly.
Anyone offer some insight into this, particularly how to transfer txt files properly?
Thanks in advance
|