文件的内容操作

<?php
//打开,追加方式
$fp = fopen("demo.txt", "a");

rewind($fp);
echo ftell($fp)."<br>";
//默认在文件末尾追加
fwrite($fp, "hello world\n");
//关闭
fclose($fp);


//打开
$fp = fopen("demo.txt", "r");

echo ftell($fp)."<br>";
fseek($fp, 4);//将指针向后移动4个位置
echo ftell($fp)."<br>";//返回当前指针位置
echo fread($fp, 11)."<br>";
echo ftell($fp)."<br>";
fseek($fp, -3, SEEK_END);//从最后往前移动3个字节,换行也是一个字节
echo fread($fp, 3);
rewind($fp);//直接将文件指针指向开头
echo ftell($fp)."<br>";
//关闭
fclose($fp);


//打开
$fp = fopen("demo.txt", "r");

while(!feof($fp)) {
//读取文件,一次读取n个字符
echo fread($fp, 1024);
}
//关闭
fclose($fp);


//打开
$fp = fopen("demo.txt", "r");

//如果文件出错或者到文件结尾返回真
while(!feof($fp)){
//读取文件,一次读一个字符
echo fgetc($fp);
}
//关闭
fclose($fp);


//打开
$fp = fopen("demo.txt", "r");

while(!feof($fp)) {
//读取文件,一次读一行
echo fgets($fp)."<br>";
}
//关闭
fclose($fp);

 

posted on 2016-03-28 20:54  奋斗的寒霜  阅读(139)  评论(0编辑  收藏  举报