Page 2 of 2
Re: Ideas to preserve integrity of data in flatfile
Posted: Tue Nov 04, 2008 2:16 pm
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?
Re: Ideas to preserve integrity of data in flatfile
Posted: Tue Nov 04, 2008 3:32 pm
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-" )