By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,254 Members | 2,418 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,254 IT Pros & Developers. It's quick & easy.

How do I save a text file?

P: n/a
Hi all

I want to save the contents of a textarea within my webform as a text file .txt
on my webserver. In addition, I want to save it with the same file name as the
next autoincrement number in my sql table.(e.g. If I have 15 records in my table
then the next autoincrement number will be 16 when I add a new record and I want
the text file to be saved as 16.txt)

sadly this is far as I have got cos im not sure how to do either of the above.

<?php
$textfilecontents=$_POST['text'];
?>

Any help greatly appreciated

Regards
Dynamo

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Dynamo wrote:
I want to save the contents of a textarea within my webform as a text file .txt
on my webserver. In addition, I want to save it with the same file name as the
next autoincrement number in my sql table.(e.g. If I have 15 records in my table
then the next autoincrement number will be 16 when I add a new record and I want
the text file to be saved as 16.txt)


If you're actually putting it into the table, you can get the ID by
calling mysql_insert_id() after inserting it into the table.

So something like this should work:

function insert_and_write($data) {
if (mysql_query("INSERT INTO `table` (fieldname) VALUES ('$data')",
$link)) {
$filename = mysql_insert_id().".txt";
$fp = fopen($filename, 'w');
fwrite($fp, $data);
fclose($fp);
return true;
}
return false
}

This will also prevent it from trying to write to the file if the INSERT
query fails. I haven't tested this, but I'm pretty sure mysql_query()
returns false if the query fails for some reason.
Roy W. Andersen
--
ra at broadpark dot no / http://roy.netgoth.org/

"Hey! What kind of party is this? There's no booze
and only one hooker!" - Bender, Futurama
Jul 17 '05 #2

P: n/a
Dynamo wrote:
Hi all

I want to save the contents of a textarea within my webform as a text file
.txt on my webserver. In addition, I want to save it with the same file
name as the next autoincrement number in my sql table.(e.g. If I have 15
records in my table then the next autoincrement number will be 16 when I
add a new record and I want the text file to be saved as 16.txt)

sadly this is far as I have got cos im not sure how to do either of the
above.

<?php
$textfilecontents=$_POST['text'];
?>

Any help greatly appreciated

Regards
Dynamo


Hi Dynamo,

Wouldn't it be easier for you to store that text in the database itself?

That aside.

This is how to do that:
1) Get the next autoincrement.
(I have no clue about the excact syntax because you din't mention the
database.)

For now I take that you find that number and store it in $ai, ok?

2) Create a new file with the right name ($ai.txt).
to do this:
- go to www.php.net
- look for the function fopen. (And when you are there, you can find all I
write here too, better, with more examples)

resource fopen ( string filename, string mode [, bool use_include_path [,
resource zcontext]])

So:
$filename = $ai.".txt";
$dir = "/home/dynamo/myfiles/"; // adjust to where you want to save
$fullpath = $dir.$filename;
$handle = fopen ($fullpath, "w+");

// now write
fwrite($handle, $_POST["text"]);

// and be a nice and close:
fclose($handle);
It is all on www.php.net
This is a good startingpoint:
http://nl3.php.net/manual/en/ref.filesystem.php

Please note that I didn't implement any errorhandling.
But you should.

Regards,
Erwin Moller

PS: I didn't test the code.
Jul 17 '05 #3

P: n/a
In article <41**********************@news.xs4all.nl>, Erwin Moller says...
Hi Dynamo,

Wouldn't it be easier for you to store that text in the database itself?

That aside.

This is how to do that:
1) Get the next autoincrement.
(I have no clue about the excact syntax because you din't mention the
database.)

For now I take that you find that number and store it in $ai, ok?

2) Create a new file with the right name ($ai.txt).
to do this:
- go to www.php.net
- look for the function fopen. (And when you are there, you can find all I
write here too, better, with more examples)

resource fopen ( string filename, string mode [, bool use_include_path [,
resource zcontext]])

So:
$filename = $ai.".txt";
$dir = "/home/dynamo/myfiles/"; // adjust to where you want to save
$fullpath = $dir.$filename;
$handle = fopen ($fullpath, "w+");

// now write
fwrite($handle, $_POST["text"]);

// and be a nice and close:
fclose($handle);
It is all on www.php.net
This is a good startingpoint:
http://nl3.php.net/manual/en/ref.filesystem.php

Please note that I didn't implement any errorhandling.
But you should.

Regards,
Erwin Moller

PS: I didn't test the code.


Thanks for that. Tried it and now working OK

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.