Best practice - Form submission / download file / PHP / Txt


I have been working on a project for some time and I have overcome a lot of the obstacles, however I have a problem which I can think of a few solutions, but they don’t seem ideal.

I have a textarea which upon submit should cause the contents to be downloadable as a .txt file to the end user.

I understand (now) how to con the headers (for ie and such) to download a file (Content-Disposition:) (or html5 <a href="/images/myw3schoolsimage.jpg" download>) but this requires a physical file to be there.

I understand that I can create a file using php from a form. But this means every form submission will need a new text document or share 1 and this could be a very bad practice to open a server to that kind of interaction or cause massive use-ability issues.

I could get the information from a MySQL database, but I think I would still have to write a file.

So what is the solution?

It seems a simple premise to want a form that a user can download a copy of their output effectively, but I feel like im missing a fundamental answer.

You can’t avoid to create the txt file. As you say, the result of inserting the text in the textarea is that the user downloads a txt file. So you’ll have to create it or… create it.

I’d personally create one file per text and I would delete those files when the session is ended

1 Like

Understood. Your helps appreciated :smile:

I thought that might be the case, but wasnt sure if there was some automagic way to do it.

The only thing that I can think of is JavaScript but it’s been a while since I used it so I think that @cpradio or maybe @mittineague or @ralphm can add a bit more to this.

JavaScript is frontend language (exception made whn you use a framework like node.js, of course) unlike PHP but it is limited to what the browser allows it to do and generally writing files (except for cookies) is not one of them.

Oooh … sometimes it just takes the right pointer… this might work…


You technically don’t have to save it as a file in PHP, you simply need to write headers that tell the browser to download the content instead of serving it up as a webpage.

Something along the lines of:

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=example.txt');
header('Pragma: no-cache');
echo "Line 1\r\n";
echo "Line 2\r\n";
1 Like

Ahh so you can do the same in php effectively. Will have to fiddle and see what works best. Still thanks for the pointers.

Then I do apologize that I mislead you. I’m ashamed now… well, no, not really. But I’ve learnt something new :wink:

Next time I’ll be better :smiley:

1 Like

You were still key to the discovery process :smile:

1 Like

Only because I know who to tag :smiley:


@cpradio @molona Would it be cheeky of me to ask if you know a way to reverse the process effectively.

So I would import the contents of a local txt file to the textarea. I realise I will have to have them upload the file effectively and then read it, import it in to the textarea and then delete the file.

Are you aware of any simple tutorials to do this sort of thing? or places to start?

Currently looking at : but if you know something better, im all ears.

That should be easier to do and in this case I would turn to Javascript to do the job. The reason to choose Javascript over PHP (as an example) is because to simply fill the textarea and you really don’t need to waste your web server and its processor cycles when you can “abuse” the computer of your user to do the job :wink:

Look at this question in StackOverflow

I think it has what you need

w3schools is not my recommendation. It used to be a great site and I guess it is ok for a beginner but they’re having a hard time to keep up to date with best practices.

The code looks simple and ok

Wow, I hadnt heard about that.
I thought they were kinda the “go to place” for up to date information. I will have to re-evaluate :smiley:

In the meantime will look in to javascript solution, im not sure that links quite what im after or I just dont understand how to apply it, but thats all I need for now thanks again!

Just in case anyone ends up here: I got a script to work reading a local file using javascript.
It works in Chrome:

However this does not work in IE and typically … its IE I need to have it work in.
I get a strong feeling from what I have read over the last few hours that if I want it to work in IE, I will need to do it via an upload form.

In case anyones interested: this is where I got it from.

I think I have a better understanding now of the one you recommended @molona going to give that another go.

1 Like

Reading local files with javascript can be tricky due to security concerns.

Best to stick with the process.

And as an FYI, you wouldn’t have to delete the file, if you never move it out of the temp directory… You “can” simply read it, and leave it there to be cleaned up by the file system. Granted, if there is any sensitive information in it, move it to a folder of your choice, read it, then delete it.

@cpradio actually noted this from the options as I read more about it, but wasn’t sure about how often tmp files get cleansed. Its my server so I have no excuse, I will have to find out :wink:

I think I will take the php route, although on consideration, I may be adding functionality for no good reason :blush: the user could just cut n paste in to the text area, making the whole idea of importing from a txt file mildly pointless. Will have a think about it and see if I can find any justification for implementing it.

Getting a strong feeling I went snow-blind there for a bit.



The fact that you are questioning yourself is a good sign. Always ask yourself if you are taking the correct route for a given problem. It will save you a lot of time in the future!