add, create, delete data in XML file

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
php-forum Super User
php-forum Super User
Posts: 1803
Joined: Thu Mar 10, 2011 5:07 pm

Thu Sep 27, 2012 8:57 am

New php-forum User
New php-forum User
Posts: 13
Joined: Thu Sep 27, 2012 10:55 am

Thu Sep 27, 2012 11:13 am

Personally, I wouldn't use a flat file for data storage if you intent to change the data often. Flat file is great for content management system where the information changes once in a while. A database back end would be faster and less line of code to do the same thing.

That said, here's a function I use to write page content to a text file. XML will have additional nuances, but this will get you in the right direction, unless I'm totally misunderstanding your question.

Code: Select all

public function writer($fpath, $string){
	$date = date('jS F Y');
	$handle = fopen("$fpath", 'a'); //file path to write, a - append on to end
	flock($handle,LOCK_EX); //lock the file so only one user can write to it at one time.
	$input = $date.",".$string."\n"; //concatinate variables in to one string
	if ($input > 0)
	    fwrite($handle, $input);
	    return true;
	    flock($handle, LOCK_UN);// un lock the file
	   else die("No lines to write for $fp");
     } //closes function writer
So this opens your file and appends the string of data on to the end of the opened file. You would use this to add items.

Other functions to look at fseek(), fgets(), fgetc(), fclose().
Hope that helps

Post Reply