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
|
|
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
|
|
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.
| Discuss Tutorial: Flat File Databases | 32 Comments |


otherwise I might have never got it done 

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