Ideas to preserve integrity of data in flatfile

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

hknight
Forum Newbie
Posts: 13
Joined: Wed Aug 13, 2008 2:48 pm

Re: Ideas to preserve integrity of data in flatfile

Post by hknight »

jshpro2, I am not worried about one person overwriting another person's data. I am only worried about the data file becoming corrupted so it does not contain any good information or it contains twice the information that it should. Would my code prevent the data from being corrupted?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Ideas to preserve integrity of data in flatfile

Post by josh »

Code: Select all

 
<?php
 
$clear = fopen('file.txt','w');  
fwrite($clear,'');
fclose($clear);
 
$a = fopen('file.txt','a');
 
fwrite($a,'a1-');
fwrite($a,'a2-');
fwrite($a,'a3-');
fclose( $a );
 
readfile('file.txt');  // outputs a1-a2-a3-
 
echo '<br />';
 
 
$clear = fopen('file.txt','w');  
fwrite($clear,'');
fclose($clear);
 
$a = fopen('file.txt','a');
$b = fopen('file.txt','a');
 
fwrite($a,'a1-');
fwrite($b,'b1-');
 
fwrite($a,'a2-');
fwrite($b,'b2-');
 
fwrite($a,'a3-');
fwrite($b,'b3');
 
fclose( $a );
fclose( $b );
 
readfile('file.txt'); // outputs a1-b1-a2-b2-a3-b3
 
In the first example client A runs fine.
Client A may crash if it saw Client B's data as demonstrated in the second example, or worse it might not crash but just reveal sensitive info to another client ( where A reads "a1-b1-a2-b2-a3-b3" instead of "a1-a2-a3-" )
Post Reply