打开文件
<?php header('content-type:text/html;charset=utf8'); $f1=@fopen("test.txt","rb") or die("文件不存在");//只读的方式打开文件,不会创建文件,为了文件安全通常带b(二进制) $f2=@fopen("test.txt","w+b");//读写的方式打开,不存在就创建,文件的大小截为0 @fclose($f1); @fclose($f2);//关闭资源
删除文件
@unlink("test.txt");//删除文件
写入内容
<?php header('content-type:text/html;charset=utf8'); $f2=@fopen("test.txt","wb");//读写的方式打开,不存在就创建,文件的大小截为0 $res=fwrite($f2,'hello world'); var_dump($res);//int 11 @fclose($f2);
读取文件
<?php header('content-type:text/html;charset=utf8'); $f2=@fopen("test.txt","r");//读写的方式打开,不存在就创建,文件的大小截为0 $res=fread($f2,1024);//读取文件 echo $res;//hello world @fclose($f2);
读取文件全部内容
<?php header('content-type:text/html;charset=utf8'); $f2=@fopen("test.txt","r");//读写的方式打开,不存在就创建,文件的大小截为0 //读取全部内容 while (!feof($f2)) {//判断资源是否到末尾 echo fread($f2, 1024);//读取文件 hello world hello world hello world } @fclose($f2);