This is the test script That I lost data.
This script just dose read and write back same data into same file. Nothing more than that.
At Location 1, the script read one data line where $wid exist stor into $DatalineMove.
At Location 2, the script reinsert same data into randam location (randam $i postion).
At Location 3, the script just reread and rewrite. At this postion, it dose nothing. just read nad write.
I set unset() between each steps becouse I just wanted to make sure.
And all read and write proseses looped for 50 time, wating about micro secounds each steps.
While reloading sometimes, Data will be lost.
set_time_limit (0);
$LIMIT = 50;
$FileName = "./TestLog.log";
$wid = "TestLogLine";
for( $T=0 ; $T < $LIMIT ; $T++ ){
srand((double)microtime()*1000000);
$rand = rand( 0 , 10000 );
usleep( $rand );
print( "Loop # $T"." Sleep $rand <BR>" );
//Location 1
//Get One line from Data file ********************
$line = file( $FileName );
for( $i=0 ; $i < count($line) ; $i++ ){
$frame = explode(" + ", $line[$i]);
$array = explode(":-:", $frame[5]);
if( trim($array[1]) == $wid ){
$DatalineMove = $line[$i];
}
}
//Get One line from Data file ********************
unset($line);
unset($frame);
unset($array);
//Location 2
//Insert into Randam Location ********************
$line = file( $FileName );
srand((double)microtime()*1000000);
$rand = rand( 0 , count($line) );
for( $i=0 ; $i < count($line) ; $i++ ){
$DataToSave .= $line[$i];
if( $i == $rand ){
$DataToSave .= $DatalineMove;
}
}
if( $fp = fopen ( $FileName , "w" ) ){
if(flock( $fp , LOCK_EX )){
fputs ($fp , $DataToSave);
flush( $fp );
flock( $fp , LOCK_UN );
fclose ( $fp );
}
}
//Insert into Randam Location ********************
unset($line);
unset($rand);
unset($DatalineMove);
unset($DataToSave);
unset($fp);
//Location 3
//Just Read & Write Test ************************
$line = file( $FileName );
for( $i=0 ; $i < count($line) ; $i++ ){
if($line[$i]){
$DataToSave .= $line[$i];
}
}
if( $fp = fopen( $FileName , "w" ) ){
if( flock( $fp , LOCK_EX ) ){
fputs ( $fp , $DataToSave );
flush( $fp );
flock( $fp , LOCK_UN );
fclose( $fp );
}
}
//Just Read & Write Test ************************
unset($line);
unset($DataToSave);
unset($fp);
}
Brandon Schnell wrote:
its a poorly written script.
post a short example of the code that you're using to read & write files.