note 37014 added to function.flock

From: Date: Thu, 30 Oct 2003 20:05:27 +0000
Subject: note 37014 added to function.flock
Groups: php.notes 
Request: Send a blank email to php-notes+get-59536@lists.php.net to get a copy of this message
A poster above (below?) has the right idea with writing to a tempory file, then copying. However, achieving the 'right' to use the temp file in the first place is still a race condition issue: if (file_exists($temp_filename)) { $fd = fopen ($temp_filename, 'w'); } is still wrong, there's still a race condition between checking to whether the temp file exists and creating it. Using the 'x' option for opening is a way to avoid this. With the 'x' option chosen, the fopen will fail (returning FALSE) if the file already exists. $fd = fopen ($tmp, 'x'); if ($blocking == 1) { while ($fd === FALSE) { clearstatcache(); usleep(rand(5,70)); $fd = fopen ($tmp, 'x'); } } ---- Manual Page -- http://www.php.net/manual/en/function.flock.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+37014 Delete -- http://master.php.net/manage/user-notes.php?action=delete+37014&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+37014&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#59536) next »