1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

PHP and Flat File Data Storage

Using PHP Commands with Flat File Text (txt) Documents


When working with data in flat files, the first thing you must do is open the file. This same function will also create the file if it does not already exist. Once open you can do several things with the contents.

Fread () is used to read the contents of the file for as many bytes as you specify, and return the data to the PHP file.

The file () command retrieves data from a flat file, and puts it into an array that you can the use in your PHP script.

Similar to file () is file_get_contents () which also reads the file, but instead of creating an array, returns all the data in a string.

Another way to read a file is line by line. This is how it is read if you use the fgets () function. It then leaves the pointer at the end of the last line read.

If you want to add data to the file you can do so using the fwrite () command. This will write directly from the current position of the pointer.

If you are writing to a file and do not wish to overwrite what is already there, you need to find the end of the file using the feof () function. This will place the pointer at the end of the last data in the file.

Related Video
Basic PHP Syntax
Using PHP With HTML
  1. About.com
  2. Technology
  3. PHP / MySQL
  4. Learn PHP
  5. PHP Flat Files - PHP Txt Files - PHP Text Files

©2014 About.com. All rights reserved.