php文件操作示例源码

<?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);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值