#native_company# #native_desc#
#native_cta#

Filehandling

By Sander
on February 4, 2003

Version: 1

Type: Full Script

Category: File Management

License: GNU General Public License

Description: This easy php-script is one I made for having an example of filehandling. You easily can view, make, edit and delete standard information without a database. Every time you add a piece of information, it’s written to a file. The only thing you have to do is change the script to your own needs. If anyone wants to make a new version: go ahead. My suggestion is a login feature.

<?
//CONFIGURATION
	//Directory waar de bestandjes worden opgeslagen (slash aan het einde)
	$filedir = "reizen/";

function title($mode){
	print("<B>Filemanager (bestand $mode)</B><BR><BR>n");
}

function kiesbestand($dir, $mode, $session){
	$handle = opendir($dir);
	while ($file = readdir($handle)){
		//check file type for txt files
		$the_type = strrchr($file, ".");
		$is_txt = eregi("txt",$the_type);
			if ($file != "." and $file != ".." and $is_txt){
			$name = str_replace(".txt", "", $file);
			print("<a href="$this_doc?file=$file&session=$session&mode=$mode">$name</A><BR>n");
			}
		}
	closedir($handle);
}

function write2file($bestemming, $duur, $kosten, $filedir){
	$strwrite = "<?" . 
	'$bestemming' . " = "$bestemming";" . 
	'$duur' . " = "$duur";" . 
	'$kosten' . " = "$kosten";" . 
	"?>";
	$strfile = $filedir . $bestemming . ".txt";
	$fd = fopen($strfile, "w");
	fwrite($fd, $strwrite);
	fclose($fd);
	print("De info is toegevoegd aan het bestand '$filedir$bestemming.txt'.<P>");
}
?><HTML>
<HEAD>
</HEAD>
<BODY>

<?
// SAVE THE FILENAME OF THIS FILE IN $this_doc
$this_doc = strrchr($PHP_SELF, "/");
$this_doc = str_replace("/", "", $this_doc);

//BEGINWEERGAVE
If (!isset($mode)){
		print("<B>Filemanager</B><BR><BR>n");
		print("<A href="$this_doc?mode=bekijken">Bestand bekijken</A><BR>");
		print("<A href="$this_doc?mode=toevoegen">Bestand toevoegen</A><BR>");
		print("<A href="$this_doc?mode=bewerken">Bestand bewerken</A><BR>");
		print("<A href="$this_doc?mode=verwijderen">Bestand verwijderen</A>");
}
//BEKIJKEN
Elseif($mode == "bekijken"){
	If(!$session){
		title($mode);
		kiesbestand($filedir, $mode, 1);
	}
	Else{
		title($mode);
		include($filedir . $file);
		print("Bestemming: <B>$bestemming</B><BR>");
		print("Duur: <B>$duur</B><BR>");
		print("Kosten: <B>$kosten</B><BR>");
	}
}
//TOEVOEGEN
Elseif($mode == "toevoegen"){
	If(!$session){
		title($mode);
		print("<FORM action="$this_doc" method="post">");
		print("<INPUT type="hidden" name="session" value="1">");
		print("<INPUT type="hidden" name="mode" value="$mode">");
		print("Bestemming: <INPUT type="text" name="bestemming"><BR>");
		print("Duur: <INPUT type="text" name="duur"><BR>");
		print("Kosten: <INPUT type="text" name="kosten"><BR>");
		print("<INPUT type="submit" value=" OK "></FORM>");
	}
	Elseif($session == 1){
		title($mode);
		write2file($bestemming, $duur, $kosten, $filedir);
	}
}
//BEWERKEN
Elseif($mode == "bewerken"){
	If(!$session){
		title($mode);
		kiesbestand($filedir, $mode, 1);
	}
	Elseif($session == 1){
		title($mode);
		include($filedir . $file);
		print("<FORM action="$this_doc" method="post">");
		print("<INPUT type="hidden" name="session" value="2">");
		print("<INPUT type="hidden" name="mode" value="$mode">");
		print("Bestemming: <INPUT type="text" name="bestemming" value="$bestemming"><BR>");
		print("Duur: <INPUT type="text" name="duur" value="$duur"><BR>");
		print("Kosten: <INPUT type="text" name="kosten" value="$kosten"><BR>");
		print("<INPUT type="submit" value=" OK ">");
	}
	Elseif($session == 2){
		title($mode);
		write2file($bestemming, $duur, $kosten, $filedir);
	}

}
//VERWIJDEREN
Elseif($mode == "verwijderen"){
	If(!$session){
		title($mode);
		kiesbestand($filedir, $mode, 1);
	}
	Else{
		title($mode);
		unlink($filedir . $file);
	}
}
?>
</BODY>
</HTML>