fwrite - Php Lock files when writte -
i testing code using little database in txt files. important problem have found is: when users write @ same time 1 file. solve using flock
. os of computer windows xampp installed (comment because understand flocks works fine on linux no windows) need test on linux server.
actually have tested code loading same script in 20 windows @ same time. firsts results works fine, after test database file appears empty.
my code :
$file_db=file("test.db"); $fd=fopen("".$db_name."","w"); if (flock($fd, lock_ex)) { ftruncate($fd,0); ($i=0;$i<sizeof($file_db);$i++) { fputs($fd,"$file_db[$i]"."\n"); } fflush($fd); flock($fd, lock_un); fclose($fd); } else { print "db busy"; }
how it's possible script deletes database file content. proper way: use flock
fixing of existing code or use other alternative technique of flock
?
i have re-wrote script using @lolka_bolka's answer , works. in answer question, file $db_name
empty if file test.db
empty.
Comments
Post a Comment