Page 1 of 1

[solved]on-the-fly Text file -> save to Hardisk

Posted: Sat Sep 11, 2004 3:07 am
by AGISB
Hi

I am thinking about following scenario.

I have a internal MySQL based news and message system for a member based site. Everything works fine. Now I want to offer the option to save a message as text file to the members hard disk.

How can I save a text file to disk that is created on the fly with the message data?

I hope you can point me to the right direction as I am not quit sure what to search for and therefore lacking the search terms to google for a solution.

Posted: Sat Sep 11, 2004 3:21 am
by feyd
create a forced-downloader type script that passes a plain-text file.

[php_man]header[/php_man] has details about forcing downloads and setting the content-type to text/plain

Posted: Sat Sep 11, 2004 4:42 am
by AGISB
Ok got it working but it seems to have a small problem in IE .

Here is what I did:

Code: Select all

<?php
		header('Content-type: text/plain');
		header('Content-Disposition: attachment; filename="message.txt"');
		echo "Date: $datefield\r\n";
		echo "from: $fromaddr\r\n";
		echo "to: $toaddr\r\n";
		echo "Subject: $subject\r\n";
		echo "-------------------------------------------\r\n\r\n";
		echo "$message\r\n\r\n";
		echo "-------------------------------------------\r\n";
		exit();

?>
Saving works both fine in Netscape and IE but if I choose the open option in the IE save window I get an error message as the temporary file does not get created.

I run XP with SP 2 installed.

Posted: Sat Sep 11, 2004 4:57 am
by AGISB
Found a solution. I just added the following:

Code: Select all

<?php
		header('Cache-Control: no-store, no-cache, must-revalidate');
		header('Cache-Control: pre-check=0, post-check=0, max-age=0');
		header('Content-Transfer-Encoding: none'); 
?>
and it now works fine.

Posted: Sat Sep 11, 2004 5:00 am
by AGISB
Now I just want to know what the best way to print a text file that is created on the fly?

I was thinking to create a plain text output with a javascript print function on it.

Can I directly send this file to the print handler in php?

Posted: Sat Sep 11, 2004 8:03 am
by feyd
not on someone else's computer, that I know of.

Posted: Sat Sep 11, 2004 8:11 am
by AGISB
The solution I have now is the following

I call a javascript window with the output data and call the javascript window.print() function.