note 117162 added to function.flock

From: Date: Fri, 24 Apr 2015 06:34:09 +0000
Subject: note 117162 added to function.flock
Groups: php.notes 
Request: Send a blank email to php-notes+get-202358@lists.php.net to get a copy of this message
Simple Helper for lock files creation <?php class FileLocker { protected static $loc_files = array(); public static function lockFile($file_name, $wait = false) { $loc_file = fopen($file_name, 'c'); if ( !$loc_file ) { throw new \Exception('Can\'t create lock file!'); } if ( $wait ) { $lock = flock($loc_file, LOCK_EX); } else { $lock = flock($loc_file, LOCK_EX | LOCK_NB); } if ( $lock ) { self::$loc_files[$file_name] = $loc_file; fprintf($loc_file, "%s\n", getmypid()); return $loc_file; } else if ( $wait ) { throw new \Exception('Can\'t lock file!'); } else { return false; } } public static function unlockFile($file_name) { fclose(self::$loc_files[$file_name]); @unlink($file_name); unset(self::$loc_files[$file_name]); } } if ( !FileLocker::lockFile('/tmp/1.lock') ) { echo "Can't lock file\n"; die(); } sleep(10); FileLocker::unlockFile('/tmp/1.lock'); echo "All Ok\n"; ---- Server IP: 72.52.91.14 Probable Submitter: 109.237.93.134 ---- Manual Page -- http://php.net/manual/en/function.flock.php Edit -- https://master.php.net/note/edit/117162 Del: integrated -- https://master.php.net/note/delete/117162/integrated Del: useless -- https://master.php.net/note/delete/117162/useless Del: bad code -- https://master.php.net/note/delete/117162/bad+code Del: spam -- https://master.php.net/note/delete/117162/spam Del: non-english -- https://master.php.net/note/delete/117162/non-english Del: in docs -- https://master.php.net/note/delete/117162/in+docs Del: other reasons-- https://master.php.net/note/delete/117162 Reject -- https://master.php.net/note/reject/117162 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#202358) next »