wyyy721 发表于 2017-3-21 10:15:44

php文件处理

  读取全文件

  1 int readfile(string filename)
  直接输出文档
  2 file()函数
  array file(string filename);
  不内容写在数组中返回。
  3 file_get_contents()函数
  string file_get_contents(string filename,[,int offset[,int maxlne]])
  读取一行数据:fgets() 和fgetss()
  string fgets(int handle,[,int length])
  string fgetss(resource handle [,int length[,string allowable_tags]])//忽略html和php标记
  读取一个字符:string fgetc(resource handle)
  写入数据:fwrite()  file_put_contents()
  int fwrite(resource handle,string string [,int length])
  
  int file_put_contents(string filename, string data [,int flags])
  依次调用fopen() .fwrite()  . fclose()
  文件操作
  1拷贝文件 boolean copy(string path1,string path2)
  2 重命名  boolean rename(string filename1,string filename2)
  3 删除文件 boolean unlink(string filename)
  4 返回最后一次访问的时间以unix时间戳方式返回  int fileatime(string filename)
  5 返回最后一次修改时间 int filename(string filename)
  6 获取文件的大小(bytes)int fliesize(string filename)
  7 array pathinfo(string name[,int option])返回一个数组dirname、basename、和extension.可以通过option设置返回信息。默认返回所有信息。
  8 string realpath(string filename):返回文件的绝对路径信息
  9array stat(string filename):返回文件相关信息。大小修改时间等。
  除了file()  readfile()等几个外其他的操作需要fopen()和fclose()而文件信息不需要。
  <?PHP
$filename="test.txt";
$total=filesize($filename);
if(is_file($filename))
{
    echo "<br>文件总大小----------:".$total."<br>";
    $fopen=fopen($filename,'rb');
echo "初始指针位置:-------".ftell($fopen)."<br>";
fseek($fopen,9);
echo "使用fessk(9)函数后函数指针位置:------".ftell($fopen)."<br>";
echo "输出当前指针后面内------容:".fgets($fopen)."<br>";


if(feof($fopen))
{
    echo "当前指针指向文件末尾:------".ftell($fopen)."<br>";
}
rewind($fopen);
echo "使用rewindow--------".ftell($fopen)."<br>";
echo "输入前4个字符内容:-----".fgets($fopen,4);
fseek($fopen,6);
echo "<br/><br/><br/><br/>第二次使用fgets()函数:----".fgets($fopen,7)."<br/>";

rewind($fopen);
echo "<br/><br/><br/><br/>第二次使用fgets()函数:----".fgets($fopen)."<br/>";
fclose($fopen);


}else{
    echo "文件不存在";
    }





?>
页: [1]
查看完整版本: php文件处理