html5中文学习网

您的位置: 首页 > 网络编程 > PHP编程 » 正文

PHP文件操作方法汇总_php教程

[ ] 已经帮助:人解决问题

   这篇文章主要介绍了PHP文件操作方法汇总的相关资料,需要的朋友可以参考下YWRHTML5中文学习网 - HTML5先行者学习网

  在data文件中写入数据:YWRHTML5中文学习网 - HTML5先行者学习网

  ?YWRHTML5中文学习网 - HTML5先行者学习网

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/6/29
* Time: 17:05
*/
header("Content-type: text/html; charset=utf-8");
//write data
$f = fopen('data','w');//打开文件
fwrite($f,'Hello PHP');//写入数据
fclose($f);//关闭文件
echo 'OK';
//windows环境暂时不考虑权限问题

  写入成功后可以在页面看到“OK”YWRHTML5中文学习网 - HTML5先行者学习网

  接下来读取data文件里的数据YWRHTML5中文学习网 - HTML5先行者学习网

  ?YWRHTML5中文学习网 - HTML5先行者学习网

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/6/29
* Time: 17:05
*/
header("Content-type: text/html; charset=utf-8");
 
//read data
$f = fopen('data','r');
$content = fgets($f);
echo $content;
fclose($f);

  如果有多行数据该怎么读取?YWRHTML5中文学习网 - HTML5先行者学习网

  方法一 while:YWRHTML5中文学习网 - HTML5先行者学习网

  ?YWRHTML5中文学习网 - HTML5先行者学习网

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/6/29
* Time: 17:05
*/
header("Content-type: text/html; charset=utf-8");
$f = fopen('data','r');
//读取多行数据 while
while(!feof($f)){//feof() 函数检测是否已到达文件末尾
$content = fgets($f);
echo $content;
}
fclose($f);

  方法二 file_get_contents():YWRHTML5中文学习网 - HTML5先行者学习网

  ?YWRHTML5中文学习网 - HTML5先行者学习网

1
echo file_get_contents('data');

  以上所述就是本文的全部内容了,希望大家能够喜欢。

(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助