代码改变世界

第二章 数据的存储与检索(2)

2016-07-19 23:34  yojiaku  阅读(227)  评论(0编辑  收藏  举报

重点:

  • 知道何时读完文件:feof() 函数
  • 每次读取一行数据:fgets() 、fgetss() 、fgetcsv() 函数
  • 读取整个文件:readfile() 、fpassthru() 、file() 函数
  • 读取一个字符:fgetc() 函数
  • 读取任意长度:fread() 函数
  • 查看文件是否存在:file_exists() 函数
  • 确定文件大小:filesize() 函数
  • 删除一个文件:unlink() 函数
  • 在文件中定位:rewind() 、fseek() 、ftell() 函数
  • 文件锁定:flock() 函数(前一篇已讲)

先放一个读文件的示例:程序清单2-3 vieworder.php——用来查看订单文件的员工界面

<?php
//create short variable name
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
?>
<html>
<head>
    <title>BOb's Auto Parts - Customer Orders</title>
</head>
<body>
    <h1>Bob's Auto Parts</h1>
    <h2>Customer Orders</h2>
    <?php
    //open the file(orders.txt) ; "rb" means "only can read"
    @$fp = fopen("$DOCUMENT_ROOT/../orders.txt",'rb');
    //lock file for reading
    flock($fp, LOCK_SH);

    //judge the file is full or not
    if(!$fp){
        echo "<p><strong>No orders pending. Please try again later.</strong></p>";
        exit;
    }

    //judge when the file go to the end
    while(!feof($fp)){
        $order = fgets($fp,999);
        //read one line of content from this file each time, it will stop when counter a "\n" or "EOF" or 998B
        echo $order."<br />";
    }

    echo "Final position of the file pointer is:".(ftell($fp));
    echo "<br />";
    rewind($fp);
    echo "After rewind, the position is:".(ftell($fp));
    echo "<br />";

    //release the file
    flock($fp,LOCK_UN);
    fclose($fp);
    ?>
</body>
</html>
View Code

然后我逐行分析:

    while(!feof($fp)){
        $order = fgets($fp,999);
        //read one line of content from this file each time, it will stop when counter a "\n" or "EOF" or 998B
        echo $order."<br />";
    }

   这里使用了功能为“每次读取一行数据”的fgets()函数,和功能为“判断何时读完文件”的feof()函数,

   先介绍这段代码的意思:利用while循环,当文件指针fp没有到达文件末尾时,每次读取一行,当文件指针读取到"\n"/"EOF"/998B时,在vieworder.php界面上打印一个换行。

   小小的TIP:

      • Web浏览器不会表示空格,例如新航,因此我们最好用HTML中的换行符<br />替换文本中的行结束符。
      • 999:这里是可读取的最大长度为指定的长度(我们自己按实际情况规定)减去1B,即998B。
  • feof() 函数:测试文件指针是否到了文件结束的位置——bool feof(resource $handle)

       handle:表示文件指针,本例中的handle是"$fp"。

       如果该文件指针指向了文件末尾,feof()函数将返回true。

       foef ==File End Of File 。

  • 每次读取一行数据:fgets() 、fgetss() 、fgetcsv() 函数
      • fgets() :从文件指针中每次读取一行内容——string fgets(resource $handle [, int $length])

            handle:文件指针是必须的,必须指向由fopen()或fsockopen()成功打开的文件(并还未由fclose()关闭)

            length:从handle指向的文件中读取一行并返回长度最多为 length - 1 字节的字符串。碰到换行符(包括在返回值中)、EOF 或者

                 已经读取了 length - 1 字节后停止(看先碰到那一种情况)。如果没有指定 length,则默认为 1K,或者说 1024 字节。

      • fgetss():从文件指针中读取一行并过滤掉HTML标记——string fgets(resource $handle [, int $length [, string $allowable_tags ]]) 

            allowable_tags:可选的第三个参数指定哪些标记不被去掉 

      • fgetcsv():从文件指针中读入一行并解析CSV字段——array fgetcsv ( resource $handle [, int $length = 0 [, string $delimiter = ',' [, string$enclosure = '"' [, string $escape = '\\' ]]]] )

            delimiter:设置字段分界符(只允许一个字符)

            enclosure:设置字段环绕符(只允许一个字符)

            escape:设置转义字符(只允许一个字符),默认是一个反斜杠

            返回包含读取字段的索引数组

 

    echo "Final position of the file pointer is:".(ftell($fp));
    echo "<br />";
    rewind($fp);
    echo "After rewind, the position is:".(ftell($fp));
    echo "<br />";

  这里使用了功能为“在文件中定位”的ftell()函数和rewind()函数,

  先介绍这段代码的意思:打印出文件指针最后的位置--换行--将文件指针复位到文件的开始--打印复位后,指针的位置--换行

  • 在文件中定位:rewind() 、fseek() 、ftell() 函数
      • rewind():倒回文件指针的位置(即将handle的文件位置设为文件流的开头)——bool rewind(resource $handle)
      • fseek():在文件指针中定位——int fseek ( resource $handle , int $offset [, int $whence = SEEK_SET ] )

            offest:偏移量。要移动到文件尾之前的位置,需要给offset传递一个负值,并设置whence为SEEK_END。

            whence:

              【1】SEEK_SET-设定位置等于offset字节

              【2】SEEK_CUR-设定位置为当前位置加上offset

              【3】SEEK_END-设定位置为文件尾加上offest

      • ftell():返回文件指针读/写的位置——int ftell ( resource $handle )        

本例在浏览器上实现效果如下: