最近一个项目有一个公告的和一个警告,需要添加,当时想到的是用数据库,存取数据,但是有点麻烦,个人觉得直接创建一个文件来保存和读取数据会更简单点,
写入时使用到了fopen()函数,fwrite(),fclose(),读取时使用到了fopen(),fread(),fclose()函数,
fopen()函数介绍
http://www.w3school.com.cn/php/func_filesystem_fopen.asp
http://www.w3school.com.cn/php/php_ref_filesystem.asp
以下是代码:
写入:
$myfile=fopen('gongGao.txt','w');
$txt="我这个是简单的一个公告";
fwrite($myfile,$txt);
$res=fclose($myfile);
//var_dump($res);
读取:
header("content-type:text/html;charset=utf-8");
$myfile = fopen("gongGao.txt", "r") or die("Unable to open file!");
var_dump(fread($myfile,filesize("gongGao.txt")));
简单记录一下,