免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1504 | 回复: 0

13 十章 文件操作 [复制链接]

论坛徽章:
0
发表于 2011-12-22 08:54 |显示全部楼层

  1. <?php
  2.    
  3.     // r 只读。文件指针置于文件开头
  4.     // r+ 读写。文件指针置于文件开头
  5.     // w 只写。在写入前,删除文件内容,将指针返回到文件开头,如果文件
  6.         //不存在,则创建
  7.     // w+ 读写。在读取或写入前,删除文件内容,如果文件不存在,则创建
  8.     // a 只写。文件指针位于文件末尾。如果文件不存在,则创建。
  9.     // a+ 读写。文件指针置于文件末尾。如果文件不存在,则创建。
  10.     // b 以二进制模式打开文件
  11.     // t 以文本模式打开文件
     // feof 文件结束
  1.     //open a txt
  2.     $fh = fopen("/usr/local/apache/htdocs/users.txt","rt");
  3.     //当没有到达结束eof
  4.     while(!feof($fh))
  5.         echo fgets($fh);
  6.     fclose($fh);
  7.     echo "<br />";

  8.     //1 将文件读入数组 file
  9.     echo "<br />";
  10.     $users = file("users.txt");
  11.     foreach($users as $user)
  12.     {
  13.         list($name,$email) = explode(" ",$user);
  14.         echo "name is :$name, email is :$email <br />";
  15.     }
  16.     echo "<br />";

  17.     //2 将文件内容读入字符串变量 file_get_contents
  18.     $userfile = file_get_contents("users.txt");
  19.     $users = explode("\n",$userfile);
  20.     foreach($users as $user)
  21.     {
  22.         list($name,$email) = explode(" ",$user);
  23.         echo "name is $name,email is :$email <br />";
  24.     }
  25.     echo "<br />";

  26.     //3 将CSV文件读入数组 fgetscsv

  27.     //读取制定数目的字符 fgets
  28.     $fh1 = fopen("users.txt","rt");
  29.     while(!feof($fh1))
  30.         echo fgets($fh1);
  31.     fclose($fh1);
  32.     echo "<br />";    

  33.     //4 从输入中剔除标记 fgetss()

  34.     //5 以一次读取一个字符的方式读取文件 fgetc()

  35.     //6 忽略换行符 fread()
  36.     echo "<br />";
  37.     $file = "users.txt";
  38.     $fh2 = fopen($file,"rt");
  39.     $userdata = fread($fh2,filesize($file));
  40.     echo "$userdata <br />";
  41.     fclose($fh2);

  42.     //7 读取整个文件 readfilea 立即输出到输出缓冲区 返回字节数
  43.     echo "7 < br />";
  44.     $file1 = "users.txt";
  45.     $byte = readfile($file1);
  46.     echo "<br />";
  47.     echo "$byte <br />";

  48.     //8 根据预定义的格式读取文件 fscanf
  49.     $fh3 = fopen("socal.txt","r");
  50.     while($user = fscanf($fh3,"%d-%d-%d"))
  51.     {
  52.         list($part1,$part2,$part3) = $user;
  53.         printf("part1:%d part2:%d part3:%d <br />",$part1,$part2,$part3);
  54.     }
  55.     fclose($fh3);

  56.     //9将字符串写入文件 fwrite()
  57.     echo "<br />";
  58.     $subscriberinfo = "jason glimore | gason@example.com";
  59.     $fh4 = fopen("users.txt","at");
  60.     fwrite($fh4,$subscriberinfo);
  61.     readfile("users.txt");
  62.     fclose($fh4);
  63.     echo "<br />";
   
     //10 将文件指针偏移量指定的位置 fseek(resource handle, int offset, SEEK_CUR)
          SEEK_CUR :当前的位置加上 offset
          SEEK_END :结尾的位置加上 offset
          SEEK_SET :设置指针位置为offset处
  
     ///11 ftell() 获取当前指针的偏移量
     //12  rewind()  将文件指针移回至文件开始处

  1.       
  2.       fopendir  打开目录句柄
  3.       closedir  关闭目录句柄
  4.       readdir  解析目录内容
  5.       scanddir 将目录读入数组  
  6. ?>



您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP