008PHP文件处理——文件操作r w (用的比较多) a x(用的比较少) 模式 rewind 指针归位:

<?php
/**
 *文件操作r w (用的比较多)  a x(用的比较少) 模式  rewind 指针归位:
 */
/*$a=fopen('a.txt','r');
echo fread($a,filesize('a.txt'));
fwrite($a,'22');//写入内容到文件最后
fclose($a);*/

//只写模式:w  如果文件存在,直接把指针放在开头,清空文件。如果不存在,则创建文件,把指针放在开头
/*$a=fopen('a.txt','w');*/

//读写模式:w+
/*$a=fopen('a.txt','w+');
fwrite($a,'蓝天科技');//文件写入内容
rewind($a);//定位到指针开头部分:
echo fread($a,filesize('a.txt'));
fclose($a);*/

//a模式:当文件不存在的时候,创建文件。存在的时候,不会像w模式,清空文件。把指针移到内容最后
/*$a=fopen('a.txt','a');
fwrite($a,'123');*/

//a+模式:读写都可以
/*$a=fopen('a.txt','a+');
echo fread($a,filesize('a.txt'));
fwrite($a,'tttt');*/

//x模式:(保守模式)如果文件存在,就不碰文件了。如果不存在就会创建文件,文件指针移到文件开头。
/*$a=@fopen('sss.txt','x') or die("文件存在");
fwrite($a,'科教兴国');
rewind($a);
echo fread($a,100);*/

//读取远端文件:
/*$baidu=fopen('http://www.baidu.com','r');
while ($row=fgets($baidu)){
    echo $row;
}*/

//wt模式:纯文本文件可以识别换行符号:
$a=fopen('a.txt','wt');
fwrite($a,'科技兴国');

  

原文地址:https://www.cnblogs.com/yiweiyihang/p/8269852.html