简体   繁体   English

多次写入文件

[英]More than one writes to file

I have a web application and when the users clicks save, a write to a file should occur. 我有一个Web应用程序,当用户单击“保存”时,应该发生对文件的写操作。 The application is "a football scheduler", thus more than one players can click on the save button at the very same time. 该应用程序是“足球调度程序”,因此多个玩家可以同时单击“保存”按钮。

Initially I thought that this would be something rare, but this is not the case, since when the match in announced players rush into, because the match will be full in a short period of time. 最初,我认为这将是一件罕见的事情,但事实并非如此,因为当宣布的球员参加比赛时,这是因为比赛会在很短的时间内完成。

What happens if two players press save at the same time? 如果两个玩家同时按下保存,会发生什么? Only the save of one player will take effect and the other one will be lost (and it is not rare that this happens with the last available position and then there is some conflict). 只有一名球员的保存会生效,而另一名球员的保存会丢失(而且这种情况很少发生在最后一个可用位置上,然后会有一些冲突)。

How can I cope with this phenomenon? 我该如何应对?

// removed code since it wasn't needed //删除了不需要的代码


I would like to know if this is possible without using a database. 我想知道如果不使用数据库就可以做到这一点。 So a no answer is also accepted. 因此,也没有答案。

I would suppose you would handle this the same way that a database would. 我想您将以与数据库相同的方式进行处理。 Add a lock to the file and wait. 向文件添加锁,然后等待。

Something like: 就像是:

while (file_exists('file.lock')) {
  usleep(10000);
}
touch('file.lock');
...
unlink('file.lock');

You may want to add a timeout. 您可能要添加超时。 I would think this would be safe enough, but I suppose it is still possible for two users to run this at the EXACT same time. 我认为这样做已经足够安全了,但是我想两个用户仍然可以同时运行它。 In that case, you may want to add a check that their content was successfully saved before continuing. 在这种情况下,您可能需要添加一个检查,以确保其内容已成功保存,然后再继续。

Update: As Marc B pointed out, file locking appears to be included in php, didn't know about this function until he pointed it out flock() . 更新:正如Marc B所指出的那样,文件锁定似乎包含在php中,直到他指出flock()才知道此功能。

$fp = fopen("file", "r+");

// try to acquire an exclusive lock, otherwise sleep 10ms before trying again
while (!flock($fp, LOCK_EX)) {  
    usleep(10000);
}

fwrite($fp, "Write something here\n");
fflush($fp);            // flush output before releasing the lock
flock($fp, LOCK_UN);    // release the lock

fclose($fp)

This would be similar to the function I wrote above. 这将类似于我上面编写的功能。 Of course don't pay attention to the fwrite, I just copied the example from php.net to show how flock() works. 当然,不要关注fwrite,我只是从php.net复制了示例以显示flock()工作方式。

It is not possible to write to the same file at the same time. 不能同时写入同一文件。

You could use a database, which can handle more than one access at the same time. 您可以使用一个数据库,该数据库可以同时处理多个访问。 In my opinion this is the best solution for handling and accessing a lot of data! 我认为这是处理和访问大量数据的最佳解决方案!

Or you could store the data in seperate files for seperate users, which are all created in a certain folder, and if there are more files available, save them one after another and delete them afterwards. 或者,您可以将数据存储在单独用户的单独文件中,这些文件都在某个文件夹中创建,并且如果有更多文件可用,请先保存一个文件,然后再删除它们。 Then keep on writing in the masterfile until no more files are available. 然后继续写入主文件,直到没有更多文件可用为止。 This would be a workaround and I really would not recommend it, but anyways it should work if you really don't want to use a database. 这将是一个解决方法,我真的不建议这样做,但是如果您真的不想使用数据库,无论如何它应该可以工作。

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

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