/* * saved 2008-09-11 from http://mechresource.myvnc.com/board/?t=12 * * chozo4's comments: Tue - Aug 01, '00 @ 12:00:00 Thought I would put this public since I'm sure there are those out there who are likely ripping their hair out trying to figure out a working method. I put a note into PHP.net's FLOCK listing (http://php.net/flock) and putting it here as well. NOTE: the chmod is set for a win9x system not NT/*nix/Etc. You'll have to adjust the chmod settings accordingly if used on other systems. Also, there are some lines that are optional which can be safely removed but will increase the chance of a lock retry when removed. Full comments provided as well. */ '; chmod($wp,0444); //set the target file to read-only fwrite($ftw,'lock'); //write the lockfile with 4bytes chmod("$wp.l",0444); //set the lockfile to read only (OPTIONAL) fclose($ftw); //close our lockfile clearstatcache(); //Clear the stat cache } function dbexport_unlock($wp,$data,$meth) { chmod($wp,0666); //Set the target file to read+write //Write the passed string to the target file then close fwrite($ftw=fopen($wp,$meth),$data);fclose($ftw); //Validate the written data ujsing a string comparison $check=file_get_contents($wp); if($check != $data) echo "Data Mismatch - Locking FAILED!
"; chmod("$wp.l",0666); //Set the lockfile to read+write (OPTIONAL) unlink ("$wp.l"); //Release the lockfile by removing it } while(1) //our stress-test loop (must run multiple instances to verify) { dblock('test'); //aquire our file lock on file 'test' (must exist) //getmypid() turns up blank on this system //use a random number instead for this test $pid=rand(0,500000); //Our contents to be written to the file $string="Important Information! From $pid"; //Unset our locks incrementally in steps while writing dbexport_unlock('test',$string,'w'); } ?>