View Full Version : Saving text file to users desktop

11-10-2006, 10:10 PM
Hi all.
I'd like to have a script that would save a text file to the users desktop (or any location they request if that not too complexed).

$filename ="text.txt";
$myFile= fopen($filename,'a');
$string="This is what is saved";
fputs($myfile, $string);

The code above will write to a file that 'already' exsits on the server but I want to generate the file from scratch and have it save on the users computer. I'll be using data from mysql db. Any help would be appreciated.


11-10-2006, 10:23 PM
Here is a simple example of how you can achieve this

// file to save is of type Microsoft Word document
header('Content-type: application/doc);

// here you can specify the name of the file...you can call it
// anything you want...
header('Content-Disposition: attachment; filename="file_name.doc"');

// now start reading the file on your server to start
// downloading to user's desktop

hope that helps...

good luck

11-10-2006, 11:26 PM
That's brilliant.

Just what I needed.


11-11-2006, 10:40 AM
Have a problem with the supplied code. Let me explain. The file I generate on the server is pure ASCII but unfortunately when the header download script run it does download a text file but wraps it in HTML (I didn't see this at first as the text editor I was using hid the HTML). The thing is the ASCII file on the server contains Quark Xpress Tag information which looks like HTML code ie <z7f"Arial"> This means that when I try to import the text file generated by the header script it would not import into Quark incorrectly as <body> is not a Quark Tag. Does that make sense? Basically I need to be able to simply download the ASCII text file without making any changes to the file itself. Any ideas.


11-11-2006, 11:32 AM
Hi Steve

try changing this line

header('Content-type: application/doc');

to the following

header('Content-type: application/txt');

and while you at it....try adding the following at the end of the script.

// this ensures the response is terminated once download is complete.

I hope that helps.

11-11-2006, 12:51 PM

I originally changed the doc type but have also now tried with the exit(); which makes no difference. It still wraps the original text file in html code. To be fair things are getting more confusing as I go on. The original output with the (unseen) html looked right I need the plain ascii text file to look like this:
<z6f"Arial Black">SONY<f"Arial"> Playstation in good condition.
<z6f"Arial Black">SONY<f"Arial"> Playstation in good condition.

So using your header I had to output this string like so:
$string = "&lt;z8fAriel Black&gt;xxxxxxxxxxxxxx&lt;fAriel&gt;xxxxxxxxxxxxx <br> \n";
The text in the downloaded text file did view correctly in the text editor (as above) I was using but importing it as ascii text into quark showed that not only was it wrapped in html but each line looked like the above string with the &lt and not the < symbol.

It appears that in the text editor I can get the text to 'look' how I want it but the file is not really composed how it looks.

Sorry if that doesn't make much sense but it doesn't to me either!!


11-11-2006, 04:07 PM
Sorry but I'm so stupid.

I placed it at the bottom of my html file.. Hence grabbing all the html tags.

Saved as a file on it's own, just as php, and it works.

I have the dunce hat on and am sat in the corner! LOL


11-12-2006, 02:28 AM
Hi Steve

Well...I am glad that it worked in the end. I wasn't sure why it didn't work for you.

I ran a couple of tests on my local server on windows xp and anther on Fedora 6...(I use virtual machines for running testing...as it gives me a chance to test my code in every possible situation)...and wasn't sure why it didn't work for you.

Anyway..glad that all is well in the end.