A CMS without database

//I’d like to add, as “code” is not working here, it remains incomplete.
//In near future I’d like to complete it….

Probably PHP has more flexibility than any other web language. For instance, you need not declare data type. You name your variables according to your requirements and later use them.

PHP vastly based on array concept. Few Global arrays like $_POST, $_SERVER and $_FILE play a major role throughout the process of building an application. So far I learnt, I can assure, for small or even medium size application PHP is unparalleled.

Let us build a Simple Content Management  System in PHP that will not take any help of database instead the content will be saved in a text file in a folder you mentioned.

Suppose you’d like to save your data in a folder called “cms”. So the path will be like this : cms/data.txt.

Let us start with a form that takes user inputs. The code is simple. Data passed through post method and we’ll catch them in a separate page.
First the form.

<code><fieldset><legend>Adding Contents</legend><form method=”post” action=”add_data.php”><br>Name : <input type=”text” name=”uname” /><br>Comments : <textarea name=”txt” cols=”50″ rows=”5″></textarea><br><br><hr><input type=”submit” value=”Send” /></form></fieldset>></code>

Next add_data.php page where we add contents in a text file:
$id = 1;

if (file_exists(“data.txt”))
$fp = fopen(“data.txt”, ‘r’);
$str = fread($fp, filesize(“data.txt”));
$str_arr = explode(“|”, $str);
foreach ($str_arr as $rec)
if ($rec)

$mode = (file_exists(“data.txt”))? ‘a’ : ‘w’;
$fp = fopen(“data.txt”, $mode);
$str = $id . “–” . $_POST[‘uname’] .”–” . $_POST[‘txt’] . “|”;

fwrite($fp, $str);

header(“location: index.php”);

As you see in the last line after you add data it redirects you to the index.php page. The code is:
$fp = fopen("data.txt", 'r');
$str = fread($fp, filesize("data.txt"));
$str_arr = explode("|", $str);

echo ”

“; echo “”;foreach ($str_arr as $rec)
if ($rec)
$rec_arr = explode(“–“, $rec);
foreach ($rec_arr as $col)
echo ”
“;echo “”;
echo “Edit“;
echo ”



echo “Add data“;

I’d like to edit and delete data on index page. But that part in the next blog. Till the next one happy coding!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s