Forums

PHP

This topic is locked

Php & Text files

Posted 09 Aug 2001 20:27:32
1
has voted
09 Aug 2001 20:27:32 John Wards posted:
I need to make a script that takes input from a text box and adds it to a text file.

And an other that reads from the txt file and displays the out put.

The text file should retain the original data.

Can someone point me in the direction of a script or a tut? or even the code. It can't be difficult but i can't find any info on it

Cheers

john

Replies

Replied 10 Aug 2001 13:24:12
10 Aug 2001 13:24:12 Tim Green replied:
This kind of thing is very easy to do.

First create your form, set the action attribute to :-

<?php echo $PHP_SELF; ?>

Add this within your form tag :-

<input type="hidden" name="frmSub" value="true">

And then at the top of your page add this code (this example presumes that the text field you want to add to the text file is called theTextFileField. Change it as appropriate) :-

<?php

if ($frmSub) {
$fd=fopen("myfileandpath.txt","wb+";
fwrite($fd, $theTextFileField);
fclose($fd);
}
?>

This code is good if you want to store only one value in the text file. You will need to tweak the code accordingly, following the instructions in the PHP Manual if you want to add more information.

To retrieve the content from the text file you would use something like :-

<?php
$filename="myfileandpath.txt";
$fd=fopen($filename,"r";
$textFileContents=fread($fd,filesize($filename));
fclose($fd);
?>

Again, you will have to tweak this code if you don't want to read the whole content of the file etc.

These are just basic examples, but should get you pointed in the right direction.

Hope this helps

Tim Green

Extension & PHP TalkZone Manager
<font size=1>-------------------------------------------
<i>Please read the Forum FAQ before posting
a question to this TalkZone.</i>
-------------------------------------------
www.UDzone.com : A dynamic Dreamweaver,
Ultradev and Fireworks site for developers
by developers.
-------------------------------------------</font id=size1>

Edited by - rawveg on 08/10/2001 13:24:48

Reply to this topic