flock和读取文件到数组 [英] flock and reading file into array

查看:58
本文介绍了flock和读取文件到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将文件读入数组时是否需要使用flock()?有可能

有问题的文件在文件(''filename'')请求发出时是打开的。
。我知道flock()在使用fopen()打开文件时是必需的

是对文件的争用:


$ fp = fopen($ ctr ,''w'');

//只有在我们可以锁定文件时才写入

if(flock($ fp,LOCK_EX))

{

fwrite($ fp," 0");

flock($ fp,LOCK_UN);

}

其他

{

//再试一次......怎么样???????

}

fclose($ fp);


但是在将文件读入数组时是否需要?


$ totals = file(''visinterval'');

$ var = explode(''|'',$ totals [0]);

$ i24h = number_format($ var [0]);

$ i30d = number_format($ var [1]);

$ i365d = number_format($ var [2]);


也 - 如果试图锁定文件失败(在第一个例子中),

我该怎么重试?


提前致谢。

Do I need to use flock() when reading a file into an array? It''s possible that
the file in question could be open at the time the file(''filename'') request is
made. I realize flock() is required when opening a file with fopen() when there
is contention for the file:

$fp=fopen($ctr, ''w'');
//only write if we can get lock on file
if (flock($fp, LOCK_EX))
{
fwrite($fp, "0");
flock($fp, LOCK_UN);
}
else
{
//try again... how???????
}
fclose($fp);

but is it required when reading a file into an array?

$totals=file(''visinterval'');
$var=explode(''|'',$totals[0]);
$i24h = number_format($var[0]);
$i30d = number_format($var[1]);
$i365d = number_format($var[2]);

also - if the attempt to get a lock on the file fails (in the first example),
how do I retry?

Thanks in advance.

推荐答案

fp = fopen(
fp=fopen(


ctr,''w'');

//只有在我们可以锁定文件时才写入

if(flock(
ctr, ''w'');
//only write if we can get lock on file
if (flock(


fp,LOCK_EX))

{

fwrite(
fp, LOCK_EX))
{
fwrite(


这篇关于flock和读取文件到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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