多个用户使用PHP一次写入同一文件 [英] Multiple users write to the same file at the same time using PHP

查看:136
本文介绍了多个用户使用PHP一次写入同一文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,在同一时间,可以有多个用户同时写入同一文件.我的代码示例如下.

I have a website where at the same moment, there can be multiple users writing to the same file at the same time. An example of my code is below.

PHP 5.6.20

PHP 5.6.20

<?php
$root=realpath($_SERVER["DOCUMENT_ROOT"]);
$var=time()."|";
echo $var."<br/>";
$filename=$root."/Testing/Testing/test1.txt";

$myfile=fopen($filename,"a");
fwrite($myfile,$var);
fclose($myfile);

$myfile=fopen($filename,"r");
$contents = fread($myfile, filesize($filename));
echo $contents;
fclose($myfile);
?>

我读到在PHP中,如果多个用户试图在同一个文件中写入同一个文件,则有数据损坏的可能性,我当然不希望有可能导致某些数据损坏的代码.从长远来看.

I read that in PHP if multiple users are trying to write to the same file at the same file, there is a chance of data corruption, I of course don't want to have a code that may cause some data corruption over the long run.

我试图在浏览器上几乎同时运行以上代码,以模拟多个用户同时写入同一文件,但它不会对写入的文件产生任何错误或数据损坏,但是我仍然不确定将来的数据损坏.

I tried to run the above code at almost the same time on my browser to simulate multiple users writing to the same file at the same time, and it produced no errors or any data corruption to the file that is writing to, but I'm still not sure about future data corruptions.

我了解到可以使用flock来确保2个用户不能同时写入文件,但是由于我测试了上面的代码并且没有产生数据损坏,所以我不确定是否应该更新我的代码带有flock或保持原样.

I read that I can use flock to make sure that 2 users can not write to the file at the same time, but since I tested the above code and it produced no data corruption, I'm not sure if I should update my code with flock or just leave it as it is.

我的问题是:

1)上面的代码是否有可能损坏正在写入的文件?

1) Is there any chance of the above code corrupting the file that it's writing to?

2)如果是,使用flock是否可以解决此问题?如果是,我应该如何在上面的代码中实现flock?

2) if yes, is using flock will solve this issue? if yes, how should I implement flock in the above code ?

我知道可以通过使用数据库来解决这种不确定性,但是在这种情况下,最好使用纯文本,因此请不要建议我使用数据库.

I know that this uncertainty can be solved by using a database, but for this case, it's better to use a plain text, so please don't suggest me to use a DB.

提前谢谢.

推荐答案

对于许多以多种编程语言编写的Web应用程序,这是一个常见的理论问题.

This is a common theoretical problem for many web applications in many programming languages.

答案是肯定的,它可能会引起麻烦.但是,如果要添加到文件中的内容不是很大并且通信量不大,这是一个非常理论上的问题.当今的操作系统和文件系统已经过优化(缓存,延迟写入等),以至在您使用完文件句柄后立即关闭它们时,这种情况不太可能发生.

The answer is yes, it CAN cause trouble. However, this is a very theoretical problem, if the contents you are adding to the files aren't very big and if you don't have heavy traffic. Today's operating systems and file systems are so well optimized (Caching, lazy writing etc.) that it is very unlikely to happen, when you close your file handles immediately after using them.

如果遇到错误(在使用PHP/catch异常编写其他语言的代码之前检查访问权限),并在经过一段时间的延迟后重试,或者将缓冲区写入临时文件并与之合并,则可以添加类似缓冲区的内容另一个过程-您有几种可能.

You could add something like a buffer, if you run into an error (check access rights before writing with PHP/catch exceptions in other languages) and try again after some delay or write your buffer to a temp file and merge it with another process - you have several possibilities.

是的,flock()是一个可以满足这些目的的函数,但我认为这已经过分设计了.

And yes, flock() is a function that could be good for these purposes, but I think, that would be already over-engineered.

这篇关于多个用户使用PHP一次写入同一文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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