File managing may help you through making dynamic applications. It consists of opening, reading and closing every kind of file.
You also can use files to store information without using any kind of DataBase, like MySQL, Oracle...
In this article you will learn how to build a small simple DataBase engine.
Lesson 1: Creating a simple form
Let"s create a simple form, so we have an easy way to get some info into our DataBase. We will use some HTML, but you can use your HTML editor to do the same thing.
<?php echo "fill the form bellow with something"; echo "<br>"; echo "<form action=insert.php method=POST>"; echo "<textarea name=text></textarea>"; echo "<br>"; echo "<input type=submit value=ok>"; echo "</form>"; ?> |
Name this file form.php
Lesson 2: Inserting the text on the DataBase
We have the form, so now let's fill it and get some data in. Check www.php.net/fopen to see more information about the function we will use in the next example.
First, create a file called base.txt, with nothing inside. Just create it and upload it to your server, if you are using one.
|
<?php // Open the DataBase source file, using the mode a+ $open = fopen("base,txt", "a+");
// Write to the DataBase $sep = "<texts-separator>";
$write = fwrite($open, $sep.$text) or die("Could not write to the file");
// Close the DataBase source file $close = fclose($open);
echo "Info was sent to the DataBase sucessfully!"; ?> |
Name this file insert.php, go to form.php and fill the form in with something.
Lesson 3: Reading files, showing our DataBase
Let's show what is inside the DataBase now.
|
<?php // Open the DataBase source file $open = fopen("base.txt", "r");
// Get the DataBase full content $content = fread($open, filesize("base.txt"));
// Get result by result using <texts-separator> $content = explode("<texts-separator>", $content);
// Calculate the total results $total = count($content);
// Show the content echo "<b>DataBase content:</b><br><br>";
for($i = 1; $i < $total; $i++) { echo "(".$i.") - ".$content[$i]; echo "<br>"; } ?> |
Name this file read.php
Make your tests now. Insert some info (form.php) and go to read.php! Very simple, right? You can change this script and make things like tag board, users registering, client info and much more!
Good Luck!