...

View Full Version : Force download of text file



Spudhead
10-08-2007, 12:11 PM
I should preface this with: I'm an ASP developer, my PHP is dodgy at best and the following script is cobbled together from bits I've found online.

The script reads an encrypted text file, decrypts it, and SHOULD force the browser to save the decrypted data as a plain text file. It does everything right except the last bit. If I comment the header lines out (as I have in the code below), it writes the decrypted text to the browser.

If I leave them in, Firefox hangs for a bit and then tells me it had an error downloading the file.

I'm pretty sure this is a simple thing - I'm not writing the right headers, or something. But my PHP experience is not up to giving me a clue what.

Any ideas?


<?php

include "file_func.php";


$key = 'my secret key';
$iv = '12345678';

$cipher = mcrypt_module_open(MCRYPT_BLOWFISH,'','cbc','');

$filename = $_GET["file"];
$filepath = "../_secrethiddenfolder/".$filename;

$data=read_file($filepath);

mcrypt_generic_init($cipher, $key, $iv);
$decrypted = mdecrypt_generic($cipher,$data);
mcrypt_generic_deinit($cipher);


$decrypted = str_replace("---", "\n", $decrypted);

$filesize = strlen($decrypted);



ob_clean_all();

/*
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Transfer-Encoding: binary");
header("Content-Type: application/octet-stream");
header("Content-Length: " . $filesize);
header("Content-Disposition: attachment; filename=\"" . $filename . "\";" );
*/

echo rtrim($decrypted, "\0");

//echo $filesize;

die();


function ob_clean_all () {
$ob_active = ob_get_length () !== false;
while($ob_active) {
ob_end_clean();
$ob_active = ob_get_length () !== false;
}
return true;
}


?>

name _F1
10-08-2007, 01:06 PM
http://www.sephiroth.it/phpwiki/index.php?title=Force_download_Text_files

I've been meaning to find a solution to this same problem for a while now, and this reminded me.

I tested the above in IE7, Firefox and Opera and they all worked fine. The example doesn't set $filename, so don't forget that. Also, that example uses short tags, you may need to replace the <? with <?php.

Spudhead
10-08-2007, 03:04 PM
Well, I got an error on the "PMA_USR_BROWSER_AGENT" bit, so I took all the browser-specific stuff out and just used the non-IE code, so I could see if it worked in Firefox. Which it did. I then hit it with IE7 and the same code works fine. So thank you very much :)

michaelespinosa
10-15-2007, 10:56 PM
I have a script that I use to force download all kinds of files. You may find it of use to you in this situation. Hope it helps.


<?php

$filename = $_GET['file'];

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');

// addition by Jorg Weske
$file_extension = strtolower(substr(strrchr($filename,"."),1));

if( $filename == "" )
{
echo "<html><title>Title Here</title><body>ERROR: download file NOT SPECIFIED. </body></html>";
exit;
} elseif ( ! file_exists( $filename ) )
{
echo "<html><title>Title Here</title><body>ERROR: File not found.</body></html>";
exit;
};
switch( $file_extension )
{
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();

?>

And then when I want to a link to download a file I use


<a href="http://www.domain.com/download.php?file=filename.pdf">download pdf</a>

And make sure this file is saved as download.php and in the same directory as the file you want to download.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum