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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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 |