Navigation
Poll
Would you be an active poster if Ron's Guide had a message board?


Total Votes: 62
Comments: 18 — View
Past pollsPoll idea?
Rate Ron's Guide
Rate our resource at Bigwebmaster.com
Flat File Databases
Learn how to store and retrieve information using PHP's built in file handling functions along with serialize and unserialize.

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' => 21
);

// 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. If you don't know how to do that, you can ask in the forums.

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.

« Previous [ 1 2 3 ] Next »
Discuss Tutorial: Flat File Databases 32 Comments
Comment by Ron on May 11, 2004, 12:28 am
Please post questions or comments about this tutorial below. Smile
Comment by David on May 11, 2004, 1:24 am
Thanks for finally finishing the tut ron, now i can start with flat file Cool
Comment by Ron on May 11, 2004, 1:42 am
Thanks for bugging me about it Tongue otherwise I might have never got it done Laughing
Comment by cube on Jul 12, 2004, 9:28 pm
this sounds pretty easy.. lemme try it
Comment by Adam on Jul 23, 2004, 5:28 pm
I don't get it...
Comment by dcool on Aug 22, 2004, 2:04 am
your tut is pretty cool it will help me build better scripts thanks
Comment by Insane on Sep 13, 2004, 6:38 am
great tut ron but i have a cuestion, can i pull out the 5 last data that i get in the info file. Undecided
Comment by Derek on Sep 15, 2004, 8:43 am
How could I make it echo html. I'm trying to ake it so I can add downlaods to my site easier and I want to try and make echo into the table. Any suggestions?
Comment by Derek on Sep 16, 2004, 2:30 pm
Ok I've figured it out after a little time and effort i got it thanks to this tutorial mainly and a few others that helped me with a few things but this one mainly got me my own self-made file db.
Comment by Umar on Sep 18, 2004, 4:48 pm
Crying Please Can U Help Me. PLZZZZ, i want to make a simple User System with Registration, Login, Edit Profile, Member's List, ForGot PassWord, Remember Me Check BOx and User's Secure Area, Database in a Flat File. Please, and if you can Contact me On MSN Umar_Maqsood@hotmail.com

Thank YOu. Very MUCH Crying

« Previous [ 1 2 3 4 ] Next »
Post a comment
Sorry, you must be a registered member to post comments.

If you would like to register, you can do so here.
If you already have an account, please login.