Page 1 of 1

Create txt file on the fly

Posted: Mon Mar 13, 2006 2:26 pm
by matthijs
I want to make a web page with a form in which people can fill in some data. After submitting the form the data should be written to a file which can be downloaded by the user.

Nothing too complicated using some combination of fopen, fwrite and header() and other functions.

However, letting people write data to txt files on a live server seems like a big risk to me, maybe even after validating all data. So I was wondering, is it possible to let users create a txt file which is created the moment the form is submitted, give the file to download and in that way make sure the file is never placed on the server?

And a second question: I also looked at the creation of word documents, but as it seems the COM extension is only available on windows servers. Are there other ways to create word docs?

Re: Create txt file on the fly

Posted: Mon Mar 13, 2006 2:40 pm
by timvw
matthijs wrote:However, letting people write data to txt files on a live server seems like a big risk to me, maybe even after validating all data. So I was wondering, is it possible to let users create a txt file which is created the moment the form is submitted, give the file to download and in that way make sure the file is never placed on the server?
(untested... Offcourse you can store it first a while in a database or wherever.. Just make sure it's never interpreted by a scripting engine in order to avoid security problems...)

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

if (isset($_POST['form_text'])) {
 header('Content-Type: text/plain');
 header('Content-Length: ' . strlen($_POST['form_text']);
 print $_POST['form_text']);
 exit;
}
?>

<html>
...
<form action="#" method="post">
<div>
 <textarea name="form_text"></textarea>
 <input type="submit" name="submit" value="GO"/>
</div>

</form>
...
</html>
matthijs wrote: And a second question: I also looked at the creation of word documents, but as it seems the COM extension is only available on windows servers. Are there other ways to create word docs?
There are other ways, just stfw, i've got the feeling this area of applications will really push word-xml format... Anyway, why generate a propriatary format if there are as many open formats available?

Posted: Mon Mar 13, 2006 3:06 pm
by matthijs
(untested... Offcourse you can store it first a while in a database or wherever.. Just make sure it's never interpreted by a scripting engine in order to avoid security problems...)
Thanks. Only some brackets were missing so you were pretty close. The way your code works is a good start I think. The data is outputted directly to the browser. Only drawback I see is that users should then use "safe as txt file" to get the file as a txt file themselves. Might be better if I wrote it to a file and/or put that in a database, and then present that file as a download.
just stfw
what does stfw stand for? (just curious)
Anyway, why generate a propriatary format if there are as many open formats available?
Yeah, you are right of course. It's just that people should be able to use it. So that means it must be a common format. Word is one of them. Pdf another, but the big disadvantage in this specific case is that people are not able to change the pdf they created afterwards. Not what we want. So that leaves word and txt. Or am I forgetting other formats?