The first thing I wanted to know after learning the basics of PHP was how to store my data on a more permanent basis. PHP is all well and good but without the ability to store data between browser sessions it has limited functionality. After learning how to store and retrieve data, your PHP scripts really come to life.
This tutorial will go over the basics of the PHP Filesystem Functions by creating a simple news script. The script will take information via a form and store it in a text file which we will retrieve later on.
The first step is to set up our form:
<form method=post action=file-store.php>
<input type=submit value=Submit>
This is a pretty basic HTML form. The contents in the textarea named 'news' will be passed to file-store.php via POST. Why POST? Because the textarea could contain a large amount of information and could cause problems if we used GET.
The second step is to set up file-store.php to handle and store the data passed over from the form.
$handle = fopen("news.txt", "r+");
Again this is a pretty basic file handle script. The first thing we have to do is open the file and choose a mode. In this case I decided to use r+ which will open the file for reading and writing and places the file pointer at the beginning of the file. Below is a table from the PHP User Manual explaining the most commonly used modes.
|'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. |
The function fopen() in this case opens a file named news.txt which should be in the same directory as file-store.php
The second part of this short script is what puts the contents of the form into the text file. fwrite() is the function that will handle the writing. The first part is the resource handle which represents the file we just opened. The second part is what is actually going to be written inside the file. In this case $_POST['news'] contains all the text from the textarea of the form.
The last line closes the file.
The final step is to retrieve the contents of the file and display them.
$contents = file_get_contents("news.txt");
PHP makes things so easy sometimes. The first line stores the contents of news.txt into a string, in this case $contents. The next line displays everything inside news.txt. It's really that simple.
This is as basic as it gets when it comes to storing information in flat files using PHP. There is a lot more you can, and should, do like checking if the file exists before trying to open it, and checking for formatting problems. But that is for another tutorial.
Note: All these scripts are compatible with PHP versions 4.3.0 and newer.
PHP User Manual - Section on fwrite() which shows some more options you should consider when writing these kinds of scripts.