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.

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:
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!

« 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.