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
|
|
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
|
|
The code above will produce the following output:
Hello, my name is Ron, I am 21 years old.
That's it for this tutorial, I hope it's helped you get a better understanding of flat files. If you have any questions, feel free to discuss them here.
If you're feeling up to a challenge, try making a simple guestbook or shoutbox by combining the techniques from Accessing form data and this tutorial. Good Luck!
| 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