Competent job with files: exclusive blocking of files
So, we shall begin that such exclusive blocking of a file and for what she is necessary. Not a secret, that set of sites store{keep} the information not in databases, and in simple test files. Here we shall not argue, that it is better and worse, we shall simply talk about specificity of job to the data stored{kept} in files. These data we can have various formats, various structures, but anyhow manipulation with these data should undertake. It is necessary to understand precisely, what is it it happens sometimes more difficultly, than it seems at first sight. Really, when you test the scripts, all seems ideal: the information is added, the information leaves... But it is necessary to begin job in a network and the situation can cardinally change. In a situation when with a script a plenty of people very important works simultaneously to not lose the control over operations of job with a file. Situations when two or more people will simultaneously request operations of recording in a file are possible{probable} and there will be a serious failure which will entail loss of the information. The model of exclusive blocking prevents similar situations, "resolving" the processes working with a file and not giving them{him;it} simultaneously to carry out dangerous operations.
Usually job with a file (unsafe model without blocking a file) represents sequence of actions as: opening of a file (reception of his descriptor), job with contents, closing of a file. And in the most widespread variant looks so:
<?
$fp = fopen ("path_to_file", "a"); // OPENING
fputs ($fp, "$data\r\n"); // JOB WITH THE FILE
fclose ($fp); // CLOSING
?>
Here we have opened a file in a mode of addition of the information in the end of a file, have written down in him a portion of the information $data and then have closed it (It is supposed, that we have rights on recording in a file). This the most simple and the very first, that to us could come in a head and that we realized. If you are sure, that any problems with a file will not arise or that at you not so many visitors that something has broken this your right - can close clause{article} and live easy!;) But it is necessary to do{make} something. The site http://manlix.ru in which the forum and-or the counter of visitors constantly "falls" can serve one of examples of tragedies. I, certainly, am not taken to judge, that only incorrect job with files the matter is that, but, in my opinion, it is obvious. While the number of visitors was rather small, all correctly function, as soon as sometimes simultaneously began to appear up to 10 users simultaneously, incidents with the counter and a forum have begun. How it to overcome? It appears simply, only having added some lines in operation of job with files:
<?
$fp = fopen ("path_to_file", "a"); // opening
flock ($fp, LOCK_EX); // BLOCKING OF THE FILE
fputs ($fp, "$data\r\n"); // job with a file
fflush ($fp); // CLARIFICATION OF THE FILE BUFFER AND RECORDING IN THE FILE
flock ($fp, LOCK_UN); // REMOVAL OF BLOCKING
fclose ($fp); // closing
?>
In the given example we have opened a file for addition in him information It is supposed, that we have rights on recording in a file). Then have applied exclusive blocking and by that have made our script unique process which in a present situation has access to a file. Blocking is valid all time from performance of function flock ($fp, LOCK_EX) and before performance flock ($fp, LOCK_UN). Between these functions there are operators which performance will be "safe" for a file. Other processes can get access to a file not earlier removals of blocking. An important point is application of function fflush ($fp). Transactions of change of the data can be written down about the special file buffer and are reset{dumped} on a disk later when blocking will be already removed{will be taken already off} and again there will be a danger of failure in job. Therefore the given function we compulsorily write down changes on a disk, dumping{resetting} contents of the buffer. One more not less an important point is, opening of a file! We not casually use a mode "a" (" a + "). If recording in the beginning of a file with removal{distance} of the previous contents will be necessary for us, it is necessary to refrain from application of a mode "w" (" w + ") as clarification of a file assumes removal{distance} not only contents, but also the file with the subsequent creation similar. As this process will be executed before exclusive blocking also there is a probability of failure in job. In this case it is necessary to apply the following reception:
<?
$fp = fopen ("path_to_file", "a"); // opening
flock ($fp, LOCK_EX); // blocking of a file
ftruncate ($fp, 0); // WE DELETE CONTENTS OF THE FILE
fputs ($fp, "$data\r\n"); // job with a file
fflush ($fp); // clarification of the file buffer and zapis`v a file
flock ($fp, LOCK_UN); // removal of blocking
fclose ($fp); // closing
?>
Here we have opened a file for recording in him , then have applied exclusive blocking, and only have then applied function ftruncate ($fp, 0) which has executed so clearing of a file necessary for us of contents. Like and everything, that I wanted you to tell for the beginning! Successes to you in our hard work! Up to new meetings, up to new clauses{articles}!

|