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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

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

Post 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.
Last edited by AGISB on Sat Sep 11, 2004 8:12 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post 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.
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post 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.
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

not on someone else's computer, that I know of.
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post 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.
Post Reply