File Creator/Editor

Create a form that makes and/or adds data to files on your server. Could be useful in creating a simple guestbook and free for all links flat file database.

With this code, you can make a form that writes to a file. You can create files of just about any type, because you can define the file name. You need to be sure that you are in a Linux server and have CHMODed the directory to 777. This tutorial is broken into two parts, one for the form, and one for the action for the form. The form part is easy enough. On an HTML page, copy paste the following form. Save it in the same directory as you will want to create your page. The form has input fields, such as the file name and body of the file you are creating and the submit button:

<form method="post" action="action.php">
<input type="text" name="filename" value="File name"> <br>
<textarea name="theText" cols="35" rows="15">Some text</textarea> <br>
<input type="submit" value="Post Data">
</form>



Once you have your form done, you can make the action.php file refered to in the form tag.
The code in the action.php is just code the uses the fwrite() function in php. All you need is to have something like this:
<?PHP
$filename = $_POST["filename"];
$theText = $_POST["theText"];
$theText = stripslashes($theText);
$data = fopen($filename, "a");
fwrite($data,$theText);
fclose($data);
echo "File created or updated";
?>


What the above code code does is gets the file name you specified in the form, opens or creates it, then adds the data you entered. If the file already exists, then it'll add on to the bottom of that file. The stripslashes code is to make the html code work properly, so leave it there.