标签:
在data文件中写入数据:
1 <?php 2 /** 3 * Created by PhpStorm. 4 * User: Administrator 5 * Date: 2015/6/29 6 * Time: 17:05 7 */ 8 header("Content-type: text/html; charset=utf-8"); 9 //write data 10 $f = fopen(‘data‘,‘w‘);//打开文件 11 fwrite($f,‘Hello PHP‘);//写入数据 12 fclose($f);//关闭文件 13 echo ‘OK‘; 14 //windows环境暂时不考虑权限问题
写入成功后可以在页面看到“OK”
接下来读取data文件里的数据
1 <?php 2 /** 3 * Created by PhpStorm. 4 * User: Administrator 5 * Date: 2015/6/29 6 * Time: 17:05 7 */ 8 header("Content-type: text/html; charset=utf-8"); 9 10 //read data 11 $f = fopen(‘data‘,‘r‘); 12 $content = fgets($f); 13 echo $content;
14 fclose($f);
如果有多行数据该怎么读取?
方法一 while:
1 <?php 2 /** 3 * Created by PhpStorm. 4 * User: Administrator 5 * Date: 2015/6/29 6 * Time: 17:05 7 */ 8 header("Content-type: text/html; charset=utf-8"); 9 $f = fopen(‘data‘,‘r‘); 10 //读取多行数据 while 11 while(!feof($f)){//feof() 函数检测是否已到达文件末尾 12 $content = fgets($f); 13 echo $content; 14 } 15 fclose($f);
方法二 file_get_contents():
1 echo file_get_contents(‘data‘);
标签:
原文地址:http://www.cnblogs.com/Vito-L/p/4608330.html