How do I save a text file?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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.


Any help greatly appreciated


Re: How do I save a text file?

Dynamo wrote:
Quoted text here. Click to load it

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);
     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 / /

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

Re: How do I save a text file?

Quoted text here. Click to load it

Thanks for that. Tried it and now working OK

Site Timeline