简体   繁体   English

PHP flock():我可以在文件获取和文件放置内容周围使用它(读取-修改-写入)

[英]PHP flock(): can I use it around file-get- and file-put-contents (read-modify-write)

I want to do a read-modify-write to a file whilst I have an exclusive lock. 我想对文件进行读-修改-写操作,但是我拥有排他锁。 Clearly I can use flock() and do the sequence fopen, flock, fread, fwrite, flock, fclose, but I my code would look neater if it I could use file_get_contents and file_put_contents. 显然,我可以使用flock()并执行序列fopen,flock,fread,fwrite,flock,fclose,但是如果我可以使用file_get_contents和file_put_contents,我的代码看起来会更整洁。 However, I need to lock both processes and I was wondering if was possible to do that using flock "somehow". 但是,我需要锁定两个进程,并且我想知道是否可以使用“某种方式”的羊群来做到这一点。 The danger is, of course, that I'll write something that seems to work but doesnt actually lock anything :-) 危险当然是,我会写一些看起来可行但实际上没有锁定任何东西的东西:-)

as @jonathan said you can use the flag LOCK_EX with file_put_contents but for file_get_contents you can build your own custom function and then use it in your code . 正如@jonathan所说,可以将LOCK_EX标志与file_put_contents一起使用,但是对于file_get_contents,您可以构建自己的自定义函数,然后在代码中使用它。 the code below can be a starting point for you: 以下代码可能是您的起点:

function fget_contents($path,$operation=LOCK_EX|LOCK_NB,&$wouldblock=0,$use_include_path = false ,$context=NULL ,$offset = 0 ,$maxlen=null){
    if(!file_exists($path)||!is_readable($path)){
        trigger_error("fget_contents($path): failed to open stream: No such file or directory in",E_USER_WARNING);
        return false;
    }
    if($maxlen<0){
        trigger_error("fget_contents(): length must be greater than or equal to zero",E_USER_WARNING);
        return false;
    }
    $maxlen=is_null($maxlen)?filesize($path):$maxlen;
    $context=!is_resource($context)?NULL:$context;
    $use_include_path=($use_include_path!==false&&$use_include_path!==FILE_USE_INCLUDE_PATH)?false:$use_include_path;
    if(is_resource($context))
        $resource=fopen($path,'r',(bool)$use_include_path,$context);
    else
        $resource=fopen($path,'r',(bool)$use_include_path);
    $operation=($operation!==LOCK_EX|LOCK_NB&&$operation!==LOCK_SH|LOCK_NB&&$operation!==LOCK_EX&&$operation!==LOCK_SH)?LOCK_EX|LOCK_NB:$operation;
    if(!flock($resource,$operation,$wouldblock)){
        trigger_error("fget_contents(): the file can't be locked",E_USER_WARNING);
        return false;
    }
    if(-1===fseek($resource,$offset)){
        trigger_error("fget_contents(): can't move to offset $offset.The stream doesn't support fseek ",E_USER_WARNING);
        return false;
    }
    $contents=fread($resource,$maxlen);

    flock($resource, LOCK_UN);
    fclose($resource);
    return $contents;
}

for a little explanation : i just combine the parameters for flock with those for file_get_contents so you just need to read alittle about these two functions to understand the code.However if you don't need advanced usage of this function you can just do 稍微解释一下:我只是将flock的参数与file_get_contents的参数组合在一起,因此您只需要阅读有关这两个函数的详细内容即可理解代码。但是,如果您不需要此函数的高级用法,则可以

 $variable=fget_contents($yourpathhere);

I think this line is the same as : 我认为这行与:

 $variable=file_get_contents($yourpathhere);

Except that fget_contents will actually lock the file according to your flag... 除了fget_contents实际上会根据您的标志锁定文件...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM