PHP Filesystem Concepts

All Other Tutorials


Click on thumbnailed images to enlarge

Basic Knowledge:
1) Know how to use array() in PHP
2) Know how to export data using POST or GET
3) Know HTML to create form elements

With fopen() and fwrite() you can already write to an external file on your hard disk. Okay, that's a cool thing to know about their capabilities. Let's see the codes below:


$source = "a_file.txt";
$copy_destination = "b_file.txt";
$source_handler = fopen($source, a+) or die ("$source is missing!");
$to_source = $_REQUEST['message_box'];

fwrite($source_handler, $to_source);
copy($source, $copy_destination);

fclose($source_handler); # don't forget to close it!


It's quite straightforward don't you think? First you define two files, "a_file.txt" and "b_file.txt".

Then you open "a_file.txt" for the writing process (or die: will display an error message if the file is missing or the system faces some difficulties).

Next you retrieve messages from the textarea named "message_box".

And then you TRANSFER MESSAGES to the OPENED source file.

Finally the last line tells the programme to copy the opened source file WITH THE MESSAGES and rename it as "b_file.txt".

And of course, don't forget to close anything that you've opened :)

NOTE: The "a+" in the fopen() function means APPEND. You can put "r" for read-only or "w" for writeable. "a+" is the same as "w" except it doesn't clear your source file to 0 instead it continues writing from the bottom. More functions at

That's the basic, what next? Let's make a... TAGBOARD :) without using any MySQL Database; well of course this is not the most efficient style but should you require a quick way to let your users interact, this should be useful.

Let's edit the existing codes. This time we save it as, feeder.php


$dateformat = date("d-m-y");

$source = "a_file.txt";
$database = "load_array.txt";
$uniquefile = "$dateformat";
$file_ext = ".txt";
$uniquedest = "archive";

$open_database = fopen($database, "w+") or die ("The database is missing!");
$source_handler = fopen($source, "a+") or die ("$source is missing!");

$to_source = $_REQUEST['message_box'];
$addarrayrule = "<?php $db[$dateformat] => \"archive/$dateformat$file_ext\"; ?>";

fwrite($source_handler, $to_source);
fwrite($open_database, $addarrayrule);




1) Include the following in "load_array.txt":

<?php $db = array(); ?> and save

2) Create a blank index.php file and include the following:



foreach($db as $dbkey => $dbvalue){


You should get files written in feeder.php page displayed onto the index.php page! Neat eh? If you need to update the index.php page just type via the feeder.php page's form fields and submit using GET or POST method. There you already have the most basic TagBoard!

Let's sort your entries from Newest to Oldest. Just add this line after include("load_array.txt"):

array_multisort($db, SORT_DESC);

That's it! Now your entries are displayed from Newest to Oldest (descending)!

Now apply these functions to create something else. One tip: to delete a file use unlink(filename.txt)

Tutorial Comments

Showing latest 1 of 1 comments

Very helpful, thank you!

By taddred on Dec 22, 2009 4:01 am

Tutorial Details

Author birdman View profile
Submitted on Mar 31, 2006
Page views 7,374
Favorites 3
Comments 1

Tutorial Tags