php文件操作示例源码

来源:互联网 发布:局域网通讯软件 破解 编辑:程序博客网 时间:2024/05/14 16:33
<?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);