页面1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>文件的读写</title>
</head>
<body>
<?php
//1.文件的打开,利用fopen函数,返回值类型为resource
$file = fopen("read.txt",'a+');
//2.读取文件的一行利用fgets()
//3.利用循环和feof函数来实现整个文件的读取
//4.利用fwrite或fputs函数进行文件的写入
fwrite($file,"你好啊!!!\n");
fclose($file);
$file = fopen("read.txt",'a+');
while(!feof($file)){
echo fgets($file)."<br />";
}
//4.文件的关闭
fclose($file);
?>
</body>
</html>
页面2
<?php
/*$file = fopen("read.txt",'a+');
echo fread($file,1024);//利用fread进行文件读取
echo "<hr>";
fseek($file,0);//利用fsek对文件指针进行归0
echo fgetc($file);//读取文件一个字节的数据
echo "<hr>";
fseek($file,0);
echo fgets($file);//读取文件的一行
echo "<hr>";
fseek($file,0);
$arr = file("read.txt");//利用file函数将文件的内容以数组的形式返回,注意参数的形式
print_r($arr);
echo "<hr>";
fseek($file,0);
readfile("read.txt");//将文件内容直接读取并输出到浏览器
fclose($file);*/
/*$file = fopen("http://192.168.1.1/liuyanban/login.php",'r');//打开远程文件时,必须以只读的形式打开
while(!feof($file)){
$row = fgets($file,1024);//将远程文件的每一行读取到$row变量中
if(preg_match("/<h1>(.*)<\/h1>/",$row,$result)){
$h = $result[0];
break;
}
}
echo $h;
*/
$fp = fopen("read.txt", "w+");
if (flock($fp,LOCK_EX+LOCK_NB)) { // 进行排它型锁定
fwrite($fp,"Write something here\n");
flock($fp,LOCK_UN); // 释放锁定
} else {
echo "Couldn't lock the file !";
}
fclose($fp);
?>