Home  >  Article  >  Backend Development  >  PHP lock and unlock files example code

PHP lock and unlock files example code

怪我咯
怪我咯Original
2017-07-12 11:37:531603browse

Sometimes your php script may need thread security guarantee, such as when performing file writing operations. This article provides file locking functions and usage examples. The file locking function can also be used to obtain exclusive processing space to prevent synchronization errors in script execution.

The code is as follows:

<?php
/*
*lock_thisfile:获得独享锁
*@param $tmpFileStr 用来作为共享锁文件的文件名(可以随便起一个名字)
*@param $locktype 锁类型,缺省为false(非阻塞型,也就是一旦加锁失败则直接返回false),设置为true则会一直等待加锁成功才返回
*@return 如果加锁成功,则返回锁实例(当使用unlock_thisfile方法的时候需要这个参数),加锁失败则返回false.
*/
function lock_thisfile($tmpFileStr,$locktype=false){
    if($locktype == false)
        $locktype = LOCK_EX|LOCK_NB;
    $can_write = 0;
    $lockfp = @fopen($tmpFileStr.".lock","w");
    if($lockfp){
        $can_write = @flock($lockfp,$locktype);
    }
    if($can_write){
        return $lockfp;
    }
    else{
        if($lockfp){
            @fclose($lockfp);
            @unlink($tmpFileStr.".lock");
        }
        return false;
    }
}
/** 
*unlock_thisfile:对先前取得的锁实例进行解锁
*@param $fp lock_thisfile方法的返回值
*@param $tmpFileStr 用来作为共享锁文件的文件名(可以随便起一个名字)
*/
function unlock_thisfile($fp,$tmpFileStr){
    @flock($fp,LOCK_UN);
    @fclose($fp);
    @fclose($fp);
    @unlink($tmpFileStr.".lock");
}
?>

The code is as follows:

<?php
// 使用举例
$tmpFileStr = "/tmp/mylock.loc";
// 等待取得操作权限,如果要立即返回则把第二个参数设为false.
$lockhandle = lock_thisfile($tmpFileStr,true);
if($lockhandle){
    // 在这里进行所有需要独占的事务处理。
    // ... ...
    // 事务处理完毕。
    unlock_thisfile($lockhandle,$tmpFileStr);
}
?>

The above is the detailed content of PHP lock and unlock files example code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn