Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Flat File Databases (0 Comments)
Admin: Posted Date: March 3, 2010

PHP is a very powerful language, even without the use of a database, but you can only go so far without the need to store and retrieve data. One way to do this is by using flat files.

Flat File Databases

PHP is a very powerful language, even without the use of a database, but you can only go so far without the need to store and retrieve data. One way to do this is by using flat files.

A flat file is a .txt file used to store information on the server. Because the information is stored in a .txt file, it makes it easy for you to create, manipulate, and delete databases in seconds. There are many methods you can use to store data in flat files, but I've found serialize and unserialize to be the best.

serialize and unserialize are functions in PHP that are handy when you need to store information in flat files. serialize takes an array of data and generates a storable representation of that array. For example:

 
  <?php
  $myArray 
= array('some','useless','information');
  
$data serialize($myArray);
  echo 
$data;
?>

The above code will output the following:
a:3:{i:0;s:4:"some";i:1;s:7:"useless";i:2;s:11:"information";}

 

If you haven't worked with serialize before, it probably just looks like a bunch of gibberish. However, within that gibberish is the information that was held in the array '$myArray'. See it?

unserialized does the opposite of serialize, it creates an array from a representation(the output of serialize). PHP takes what looks like gibberish above and changes it back into an array, which you can then use as you would a normal array.

Try testing out serialize and unserialize, just to get the hang of how they're used. When you think you have a handle on them, go on to part two.

Now that you're familiar with the functions needed to make your information storeable, Let's discuss the functions you're going to need to store this information... fopen, fwrite, and fclose.

fopen()
fopen is a function built into PHP that is used to open a file or URL. fopen has two parameters that you must define; filename and mode. The filename is the path or URL to the file you want to open. The mode is the mode you want to open it in.

There are eight modes you can use to open files with fopen:

mode Description
'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.
'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.
'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.

If I haven't bored you to death and you're still with me, here is the basic outline for opening a file:

 
  <?php

$handle 
fopen('myfile.txt','w');

?>

This tells PHP to open the file specified in the first parameter, in this case 'myfile.txt', in mode 'w'. It places the file handle, or pointer, into the variable '$handle'. This is important, because it is needed for the first parameter of fwrite.

fwrite()
fwrite is another built in function of PHP, this is the function that does the actual writing to the file. fwrite also has two required parameters; handle and string. handle is the file handle that is returned by fopen($handle). string is the string or text that you want to be written to the file. Pretty simple, eh?

fclose()
finally, we have fclose, all this does is close the file pointer that was opened by fopen. Its only parameter is handle, which is the file handle that is returned by fopen($handle).

Still with me? Good... Let's look at an example of how we can store an array of information. I'll use the same array I used for the associative array example in the Utilizing Arrays Tutorial for this example.

 
  <?php

$personal_info 
= array(
                
'name' => 'Ron'
                
'site' => 'ronsguide.com'
                
'mail' => 'ron@ronsguide.com',
                
'age' => 22
);

// Serialize the array
$serialized_info serialize($personal_info);

// Write the serialized array to file
$file 'path/to/file/info.txt';
$handle fopen($file'w');
fwrite($handle$serialized_info);
fclose($handle);

?>

The above is one way you could write the data held in $personal_info to a flat file. Line 9 serializes the array using serialize (as discussed in part 1). Line 13 opens the file using fopen() in 'w' mode. Line 14 writes the serialized array to the file with fwrite(). Finally, line 15 closes the file using fclose().

Try running the code above, make sure you change line 12 to the path to your file 'info.txt'. For example, if your info.txt file is in a folder called 'data', then the path would be 'data/info.txt'. If it doesn't work try changing the file permissions on your info.txt file to 777. 

Go ahead and play with writing information to a file, when you think you've got it pretty well figured out, go on to part three and learn how you can access this stored information.

So far you've learned how to use serialize and unserialize, and how to store serialized arrays using fopen, fwrite, and fclose. However, those arrays are useless unless you can access them later.

To pull the information from the file, we could open the file using fopen, then read the data using fread, but since there is nothing else in the file except our serialized array we can use file_get_contents() instead.

file_get_contents does exactly what it says, it gets the contents of a file and stores it in the variable you define. file_get_contents has one required parameter: filename, which is the filename of the file you want to get the contents of... Pretty straightforward, eh?

How about an example?

 
  <?php

$file 
'somefile.txt';
$contents file_get_contents($file);
echo 
$contents;

?>

The above code will output everything that is in the file 'somefile.txt'. Try it out, just create a file called 'somefile.txt' and put whatever you want in it, then run the script.

Now that you know how file_get_contents works, we can use it to grab the contents of our info.txt file, which will be our serialized array. Then all we have to do is use unserialize to get the array back to normal!

Here's an example:

 
  <?php

$file 
'path/to/file/info.txt'
$contents file_get_contents($file);
$info unserialize($contents);

echo 
'Hello, my name is '.$info['name'].',
I am '
.$info['age'].' years old.';
  
?>

The code above will produce the following output:


 

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: