Using Flat Files (Writing)

Learn how to write data into a text file with PHP and flat files.

In category php.

Posted by Joseph Skidmore on 12.09.05.

Last edited on 06.01.06.

This may come in useful for those of you who have access to PHP but not any form of database. Flat files are essentially text files which store data, much like databases themselves ... the only drawback being many less functions, such as search, edit and all the other things possible with MySQL.

The first thing your going to want to do is create the form that will get the users information that we want storing, this is very easy, if you do not know how to use forms refer to my HTML forms tutorial.

<form name="input" method="post" 
action="<?php echo $_SERVER['SCRIPT_NAME']?>">

<p><label for="firstname">Name: </label>
<input type="text" name="name" id="firstname" /></p>

<p><input type="reset" name="reset" value="reset" /> 
<input type="submit" name="submit" value="submit" /></p>
</form>

This gives us an input box called Name. Simple enough information to store and retrieve. The code <?php echo $_SERVER['SCRIPT_NAME']?> simply means instead of forwarding the user to a new page to process the form details it will be handled in the same page. The PHP code goes directly below the form.

Now, we have our form, which will look like this;

The next part of the code is where we write our data into the text file (flat file). So firstly we grab the data that was entered into the input boxes.

<?php

if (isset($_POST['submit'])) {

$name = $_POST['name'];

}
?>

The if statement checks that the form has actually been submitted before running the script. The next step would be to open the file we want to store the data in:

<?php

if (isset($_POST['submit'])) {

$name = $_POST['name'];

$fp = fopen("data.txt","a");

if(!$fp) {
    echo 'Error, the file could not be opened or there was an error creating it.';
    exit;
}
}
?>

Ok, now what have we just done? fopen opens the select file, in this case 'data.txt' - At the end of fopen we have a letter, what does this letter mean? Here is a list of all the letters and their meanings;

  • 'r' - Open for reading only; place the file pointer at the beginning of the file.
  • 'r+' - Open for reading and writing; place the file pointer at the beginning of the file.
  • 'w' - Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
  • 'w+' - Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
  • 'a' - Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
  • 'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

As you can see we are using 'a' which will open our file and attempt to create the file if it doesn't already exist.

We are using an if statement to check for errors in the opening/creating of our file, if an error occurs it will output 'Error, the file could not be opened or there was an error creating it. onto the page.

Next we will write our data taken from the form onto our currently open text file.

<?php

if (isset($_POST['submit'])) {

$name = $_POST['name'];

$fp = fopen("data.txt","a");

if(!$fp) {
    echo 'Error, the file could not be opened or there was an error creating it.';
    exit;
}

fwrite($fp, $name."\n");

}
?>

Using fwrite we are writing our previously stored variable into the text file. The n tag puts the cursor onto a new line in the text file.

Now we must close the file:

<?php

if (isset($_POST['submit'])) {

$name = $_POST['name'];

$fp = fopen("data.txt","a");

if(!$fp) {
    echo 'Error, the file could not be opened or there was an error creating it.';
    exit;
}

fwrite($fp, $name."\n");

fclose($fp);
}
?>

And there you go. Your data has been sucesfully written to the text file.

Page generated in 0.0014 seconds.

Whatcounter Free Invisible Stats CounterFree invisible hit counter