Using PHP To Access Files - The Basics

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>
<textarea name=news></textarea>
<input type=submit value=Submit>
</form>

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.

<?php
$handle = fopen("news.txt", "r+");
fwrite($handle, $_POST['news']);
fclose($handle);
?>

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.

modeDescription
'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.

<?php
$contents = file_get_contents("news.txt");
echo $contents;
?>

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. 

Extra Reading

PHP User Manual - Section on fwrite() which shows some more options you should consider when writing these kinds of scripts.

References
0

I am a 23-year-old living in Blacksburg, Virginia (Virginia Tech). I have been working with the web since I learned HTML in 7th grade and have been having a lot of fun with it ever since. I work for a local design and development company called New City Media as a PHP programmer and database developer. My work-load consists of mainly writing PHP code, designing database tables in MySQL 4/5 or MSSQL 2005, general tech support for our hosting, DNS, and database servers and also the occasional tech support call for a client. Mike is a DZone MVB and is not an employee of DZone and has posted 5 posts at DZone.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

mfemia replied on Tue, 2008/12/30 - 1:36am

When I submit the form I receive the following message:


Warning: fopen(news.txt) [function.fopen]: failed to open stream: Permission denied in /.../....mysite..../file_store.php on line 2

Warning: fwrite(): supplied argument is not a valid stream resource in /.....mysite..../file_store.php on line 3

Warning: fclose(): supplied argument is not a valid stream resource in /.../....my site..../file_store.php on line 4

 

Permissions are set properly.  Can you tell me what these errors mean?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.