fopen(file,w +)在我可以检查文件是否被flock()锁定之前将其截断 [英] fopen(file,w+) truncates the file before I can check if it's locked with flock()

查看:304
本文介绍了fopen(file,w +)在我可以检查文件是否被flock()锁定之前将其截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,该函数接收文件名和json对象以写入文本文件.

I have a function which receives a filename and a json object to write to a text file.

对象已更新,需要完全替换文件的当前内容.每个站点访问者都有自己的文件.多次快速更改会导致以下情况:文件被fopen(file,w+)截断,然后由于被锁定而未被写入.最终结果是空文件.

The object is updated and needs to entirely replace the current contents of the file. Each site visitor has their own file. Multiple rapid changes create a situation where the file is truncated by fopen(file,w+), then not written to as it's locked. End result is empty file.

我敢肯定有一种简单的标准方法可以做到这一点,因为它是一种通常的活动.理想情况下,我正在寻找的是一种在w+模式下用fopen截断文件之前检查文件是否具有锁定的方法,或者是一种切换模式的方法.

I'm sure there's a standard simply way to do this as it's such a usual activity. Ideally what I'm looking for is a way to check if a file has a lock before truncating the file with fopen in w+ mode or a way to switch modes.

您似乎不得不用fopen()截断文件以使文件句柄传递给flock()来检查它是否被锁定,这似乎很奇怪-但您只是将其截断了,所以有什么用呢?

It seems strange that you would have to truncate the file with fopen() to get a file handle to pass to flock() to check if it's locked -- but you just truncated it, so what's the point?

这是我到目前为止拥有的功能:

Here's the function I have so far:

function updateFile($filename, $jsonFileData) {
    $fp = fopen($filename,"w+");
    if (flock($fp, LOCK_EX)) {  
        fwrite($fp, $jsonFileData);
        flock($fp, LOCK_UN);
        fclose($fp);
        return true;
    } else {
        fclose($fp);
        return false;
    }
}

推荐答案

PHP手册只需稍作修改即可完成您想要的操作.使用 "c"模式打开文件进行创建如果它不存在,也不要截断它.

Example #1 from the PHP manual will do what you want with a slight modification. Use the "c" mode to open the file for writing, create it if it doesn't exist, and don't truncate it.

$fp = fopen("/tmp/lock.txt", "c");

if (flock($fp, LOCK_EX)) {  // acquire an exclusive lock
    ftruncate($fp, 0);      // truncate file
    fwrite($fp, "Write something here\n");
    fflush($fp);            // flush output before releasing the lock
    flock($fp, LOCK_UN);    // release the lock
} else {
    echo "Couldn't get the lock!";
}

fclose($fp);

"c"模式的完整说明:

Full description of the "c" mode:

打开文件进行写入.如果该文件不存在,则会创建它.如果存在,则既不会被截断(与"w"相对),也不会对该函数的调用失败(与"x"的情况相同).文件指针位于文件的开头.如果希望在尝试修改建议的锁之前先获得咨询锁(请参阅 flock() ),这可能会很有用.文件,因为使用'w'可以在获得锁定之前截断文件(如果需要截断,可以在请求锁定后使用ftruncate().)

Open the file for writing. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x'). The file pointer is positioned on the beginning of the file. This may be useful if it's desired to get an advisory lock (see flock()) before attempting to modify the file, as using 'w' could truncate the file before the lock was obtained (if truncation is desired, ftruncate() can be used after the lock is requested).

它看起来好像不是您需要的,但是如果您想同时读写,还可以使用相应的"c+"模式.

It doesn't look like you need it, but there's also a corresponding "c+" mode if you want to both read and write.

这篇关于fopen(file,w +)在我可以检查文件是否被flock()锁定之前将其截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆