How do I save a text file?

This is a discussion on How do I save a text file? within the PHP Language forums, part of the PHP Programming Forums category; Hi all I want to save the contents of a textarea within my webform as a text file .txt on ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-12-2005
Dynamo
 
Posts: n/a
Default How do I save a text file?

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

Reply With Quote
  #2 (permalink)  
Old 01-12-2005
Erwin Moller
 
Posts: n/a
Default Re: How do I save a text file?

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.
Reply With Quote
  #3 (permalink)  
Old 01-12-2005
Roy W. Andersen
 
Posts: n/a
Default Re: How do I save a text file?

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
Reply With Quote
  #4 (permalink)  
Old 01-13-2005
Dynamo
 
Posts: n/a
Default Re: How do I save a text file?

In article <41e5439c$0$6218$e4fe514c@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

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:19 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0