How to Update a Text File With PHP
Updating a text file in PHP is a common task in Web development projects, as text files can act as effective, simple data stores not unlike databases. Using a text file, you can store basic data variables as well as read and write these easily. The PHP code required to update a text file is not complex, so even if you have no experience, you should be able to achieve the task quickly. The main activities involved include preparing the data, opening the file and writing to it.
Instructions
-
-
1
Prepare your text file. If you do not already have one, create your text file in a text editor and save it with ".txt" extension. Create your PHP script by opening a new file in a text editor and saving it with ".php" extension, for example "textupdate.php." Enter the following basic outline for your PHP script:
<?php
//code here
?>
Between the opening and closing PHP tags, you can insert any PHP code. Ideally, your text file and PHP script should be in the same directory.
-
2
Process the data you plan on writing to the text file. You may be acquiring your new data from a number of channels, but one of the most common is the Post variable. The Post variable allows you to pass data items between PHP scripts, as in the following example where data has been passed from another script:
$name = $_POST['name'];
This captures the passed data value and stores it in a variable. Capturing data from an HTML form is a common scenario for using the Post variable.
-
-
3
Prepare your data for writing to the file. There are various options in terms of how you format a data text file, one of the simplest involving each data value on a separate line, as follows:
Mary
Jim
Sam
If you want your data recorded in any other format, or want to append any additional data to the passed value, prepare it, as in this example where the date is also included:
$date_today = date("m.d.y");
$name .= " ".$date_today;
This would record each line in the file as follows:
Mary 12.23.11
-
4
Open the text file for writing, as follows:
$file_handle = fopen("textupdate.php", 'a');
The "a" parameter means the file will be opened for writing and any new text added to the end of it. Write your new data:
fwrite($file_handle, $name."\n");
The "\n" tells the program to insert a new line character after writing the data, so that the next write operation will start on a new line. Close the file:
fclose($file_handle);
This code works where the text file is in the same directory as the PHP script. If your is not, amend the "fopen" parameter to reflect the location of your text file relative to the PHP script.
-
5
Upload your PHP script and text file to a Web server and test them. If you are passing data to the PHP from another script, browse to that and test it using different data values. Check that your text file is being updated correctly, and that no other data is being corrupted. You can check your text file contents either by browsing to the file in your Web browser or downloading it using an FTP program.
-
1
Tips & Warnings
Sometimes when you make a connection using FTP, your text file may not look as though it has been updated according to the modification date, but when you download it you may find that it has.
Writing to a file may not work if your file permissions are not set appropriately on the Web server. If this is the case try changing the permissions using an FTP program or contact your Web host.