概述
共享锁
<?php
$file = "./jdk-8u131-linux-x64.tar.gz";
$fp = fopen($file, "r");
$starttime = explode(' ',microtime());
if(flock($fp, LOCK_SH | LOCK_NB)){
$content = '';
while(!feof($fp)){
//$content .= fread($fp, 8096);
fread($fp, 1048576);//1048576
}
//echo $content;
flock($fp,LOCK_UN);
}else{
echo "Lock file failed !";
}
$endtime = explode(' ',microtime());
$totaltime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);
echo $totaltime;
fclose($fp);
独占锁
<?php
$file = "./jdk-8u131-linux-x64.tar.gz";
$fp = fopen($file,'a+');
if(flock($fp, LOCK_EX)){
//fwrite($fp, "This is the new content;");
sleep(10);
//fwrite($fp, "the secondary write!");
flock($fp, LOCK_UN);
}else{
echo "Lock failed";
}
fclose($fp);
最后
以上就是高挑画笔为你收集整理的php文件锁的全部内容,希望文章能够帮你解决php文件锁所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复