src/Filesystem.php
public static function lockFile($filePath, $is_write_lock, $is_blocking)
    {
        global $globals;
        
        # This creates the file if it doesnt exist.
        # we have to assign the file to a global variable, otherwise the file lock will be released
        # as soon as we exit this function.
        # This MUST be a+ instead of w or w+ as using w will result in the file being wiped.
        $globals['file_locks'][$filePath] = fopen($filePath, 'a+');
        
        $lock_params = 0;
        
        if ($is_write_lock)
        {
            $lock_params =  $lock_params | LOCK_EX;
        }
        else
        {
            # read lock = shared lock
            $lock_params = $lock_params | LOCK_SH;
        }
        
        if (!$is_blocking)
        {
            $lock_params = $lock_params | LOCK_NB;
        }
        
        $result = flock($globals['file_locks'][$filePath], $lock_params);
        
        return $result;
    }
public static function lockFile($filePath, $is_write_lock, $is_blocking)
    {
        global $globals;
        
        # This creates the file if it doesnt exist.
        # we have to assign the file to a global variable, otherwise the file lock will be released
        # as soon as we exit this function.
        # This MUST be a+ instead of w or w+ as using w will result in the file being wiped.
        $globals['file_locks'][$filePath] = fopen($filePath, 'a+');
        
        $lock_params = 0;
        
        if ($is_write_lock)
        {
            $lock_params =  $lock_params | LOCK_EX;
        }
        else
        {
            # read lock = shared lock
            $lock_params = $lock_params | LOCK_SH;
        }
        
        if (!$is_blocking)
        {
            $lock_params = $lock_params | LOCK_NB;
        }
        
        $result = flock($globals['file_locks'][$filePath], $lock_params);
        
        return $result;
    }
public static function lockFile ( $ filePath , $ is_write_lock , $ is_blocking ) { global $ globals ; # This creates the file if it doesnt exist. # we have to assign the file to a global variable, otherwise the file lock will be released # as soon as we exit this function. # This MUST be a+ instead of w or w+ as using w will result in the file being wiped. $ globals [ 'file_locks' ] [ $ filePath ] = fopen ( $ filePath , 'a+' ) ; $ lock_params = 0 ; if ( $ is_write_lock ) { $ lock_params = $ lock_params | LOCK_EX ; } else { # read lock = shared lock $ lock_params = $ lock_params | LOCK_SH ; } if ( ! $ is_blocking ) { $ lock_params = $ lock_params | LOCK_NB ; } $ result = flock ( $ globals [ 'file_locks' ] [ $ filePath ] , $ lock_params ) ; return $ result ; }
Lock a file so that only we can use it.
This will NOT block untill the file can be locked, but would return false immediately if
cannot grab the lock.
Note that this only allows other processes to see that you have locked it and does not
actually guarantee that it cannot be edited at the same time on Linux.
@param String $filePath - the full path to the file that we wish to lock
@param boolean $is_write_lock - if false this will get a shared read lock, if true this
will get an exclusive write lock.
@param boolean $is_blocking - if true will wait to get lock.
@return boolean - true if we managed to lock the file, false otherwise.
Lock a file so that only we can use it . This will NOT block untill the file can be locked but would return false immediately if cannot grab the lock . Note that this only allows other processes to see that you have locked it and does not actually guarantee that it cannot be edited at the same time on Linux .