Saving data to flatfile

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
blaze
Forum Newbie
Posts: 5
Joined: Wed Sep 13, 2006 4:53 pm

Saving data to flatfile

Post by blaze »

hi all

hope someone can help with this as im totally head mashed & need a new perspective :(

Basically i have creating a php pm system that works great :D

Now in the admin CP i have an option to clean data which includes pms

all data is stored in flatfiles as i dont have sql and this script is designed for others like me.

all messages are stored in files like this for example:

pmd-Username.dat

inside this each pm is stored like this line by line:

pmcode|date|from|subject|message|status

the status part tells us if the message is 'read' or 'new'

i have got my script to seperate 'read' messages & 'new' messages

so i now have three possible options for the clean option in the admin CP

1. Clean - Read Messages
2. Clean - New Messages
3. Clean - All Messages

option 3 is done - no probs!!

the problem is option 1 or 2 - I know if i get one of them then the other is gonna be the same.

this is the code im having trouble with

Code: Select all

if ($pm == "cleanold")
{
$dir = opendir("$pmdir"); $c = 0;          //assign & open the dir for stored user pms
while ($file = readdir($dir))                   // loop through to collect all files
{
$c = $c + 1;
if ($c < 3) { continue; }
$file = explode("-", $file);            
if ($file[0] == "pmd")
{
$pmfile = "$pmdir/pmd-{$file[1]}";
$hnd = file($pmfile);
for ($x = 0; $x < sizeof($hnd); $x++)  // Open all files that start with pmd
{
list($id, $smdate, $sfrom, $ssub, $smess, $status) = explode('|', trim($hnd[$x])); 

$status = trim($status);                       // Make sure we are not processing black data

if ($status == "new")                           // Start seperation 'new' else 'read'
{
$ndata[] = $file[1]."|".$id."|".$smdate."|".$sfrom."|".$ssub."|".$smess."|".$status;
}
else
{
$rdata[] = $file[1]."|".$id."|".$smdate."|".$sfrom."|".$ssub."|".$smess."|".$status;
}
}
}
}
include ("done.htm"); exit();
}
this code works and is the building block for what i need help with, sorry if im goin on lol its hard to explain :wink:

now i have my 'new' & 'read' data, as this code is ''cleanold'' i only want to save $ndata

this is where im lost as i need to make sure i save the right messages back to the right users

note the following code

Code: Select all

$ndata[] = $file[1]."|".$id."|".$smdate."|".$sfrom."|".$ssub."|".$smess."|".$status;
}
else
{
$rdata[] = $file[1]."|".$id."|".$smdate."|".$sfrom."|".$ssub."|".$smess."|".$status;
i have already saved the username in $file[1] which = 'username.dat'

i keep looking at it and i see it lookin at me but i just can get it

any help would be much appreciated :!:

I hope you find this easy to read :oops:

ta

-::BlAzE::-
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Personally, I'd store each message seperately and use filenames to mark statuses like that. So your file would be called something like:

123.00000041.read.pm
123.00000042.read.pm
123.00000043.read.pm
123.00000044.read.pm
123.00000045.new.pm
123.00000046.new.pm

123 = user id
00000041 = message id
read = message has been read
new = message is new

When the user views a message you'd rename the file (using move() ) changing 'new' to 'read'. When the user wants to delete all their old messages you'd loop through the files in the directory you're storing them in checking the filenames .. any where the third part is 'read' get unlinked. Likewise for deleting new messages, you'd loop through the files deleting all the ones where the third part of the name is 'new'. It'd be fast as you're not opening files at all. It'd be easier than deleting specific lines from a file too (though not by much..)

Maybe make an index file too to store some info about each message (who it's from and subject line for example). Of course, then you'd be back where you are now coz you'd need to delete stuff out of the index..
blaze
Forum Newbie
Posts: 5
Joined: Wed Sep 13, 2006 4:53 pm

Post by blaze »

hi onion2k

thanks for the reply

i like the idea i dont know about the index thing not sure i get what u mean

i would store the message in the file

i'll have to give it a go and see what happens

it means i would have to re write to whole pm system to :(
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Have you considered SQLITE -- flat file data system with SQL access.

http://us3.php.net/manual/en/ref.sqlite.php
blaze
Forum Newbie
Posts: 5
Joined: Wed Sep 13, 2006 4:53 pm

Post by blaze »

hi neophyte

its not really an option

as i said.........

all data is stored in flatfiles as i dont have sql and this script is designed for others like me.
blaze
Forum Newbie
Posts: 5
Joined: Wed Sep 13, 2006 4:53 pm

Post by blaze »

anyone? :D
blaze
Forum Newbie
Posts: 5
Joined: Wed Sep 13, 2006 4:53 pm

Post by blaze »

well after many hours ive cracked it

not sure if its the best way but it works a treat!

Code: Select all

// 1st wipe all messages from all users as we have the new onces in $ndata

for ($x = 0; $x < sizeof($rdata); $x++)  // for all read messages
{
list($user, $id, $smdate, $sfrom, $ssub, $smess, $status) = explode('|', trim($rdata[$x]));

$dat = "";                          //blank data to save
$aa[] =  $user;                   // collect all users in array
foreach ($aa as $a)             // each user 1 by 1
{
$xuser = $a;
$u_path = "$userdb/pmd-{$xuser}";           // get the file
$file = @fopen($u_path, 'w');                  
@fputs($file, $dat);                  // wipe all the messages
@fclose($file);
}
}
/////////////////////////////////////////////////

// now save new messages back to members
for ($x = 0; $x < sizeof($ndata); $x++)  // for all new/unread
{
list($user, $id, $smdate, $sfrom, $ssub, $smess, $status) = explode('|', trim($ndata[$x]));
$a = $user;
${$a}[] =  $id."|".$smdate."|".$sfrom."|".$ssub."|".$smess."|".$status;
foreach (${$a} as $xzx)
{
$xuser = $a;
$u_path = "$userdb/pmd-{$xuser}";
$file = @fopen($u_path, 'w');
for ($y = 0; $y < sizeof(${$a}); $y++)
{
@fputs($file, ${$a}[$y] . "\n");
}
@fclose ($file);
}
}
//////////////////////////////////////////////////
Mission complete!!

:cheers:

-=:BlAzE:=-
Post Reply