<?php
$filepath = "wfile.txt";
echo dirname($filepath)."<br />";
//权限检测
is_writeable(dirname($filepath)) or die("can not write $filepath<br />");
is_readable($filepath) or die("can not read $filepath<br />");
//打开文件
$fhandle= fopen($filepath, "w+b");
//写数据
$i=0;
if(flock($fhandle, LOCK_EX|LOCK_NB)) //加写锁
{
fwrite($fhandle, "content1");
fwrite($fhandle, "content2\r\n");
fwrite($fhandle, "content3");
usleep(1000000);
echo $i."<br />";
$i++;
if($i==20)
break;
}
else
{
echo "can not lock ".$filepath." for write<br />";
}
//else
//读数据
if(flock($fhandle, LOCK_SH)) //加读锁
{
fseek($fhandle, 0);
while(!feof($fhandle))
{
$str = fgets($fhandle);
echo $str."<br />";
}
}
else
{
echo "can not lock ".$filepath." for read<br />";
}
//输出:content1content2|content3
//读到数组中,每行一元素
$arr = file($filepath);
var_dump($arr);
php文件操作示例源码
最新推荐文章于 2021-03-28 15:45:48 发布