search
首页课程PHP fun classRead file operation

Read file operation

目录列表

读文件内容

要读取文件里的内容,并按逐行进行读取有两种方式。

1、fgets()逐行读取文件

fgets() 函数用于从文件中逐行读取文件。

注释:在调用该函数之后,文件指针会移动到下一行。

实例

下面的实例逐行读取文件,直到文件末尾为止:

1

2

3

4

5

6

7

8

9

<?php

 $file fopen("welcome.txt""r"or exit("Unable to open file!");

 //feof()函数检测是否已到达文件末尾(EOF)。

 while(!feof($file))

 {

 echo fgets($file). "<br>";

 }

 fclose($file);

 ?>

2、使用PHP file() 函数

file() 函数把整个文件读入一个数组中。

数组中的每个元素都是文件中相应的一行,包括换行符在内。

例如:

1

2

3

4

<?php

 $arr = file("test.txt");

 print_r($arr);

?>

结果:

1

2

3

4

5

6

7

Array

 (

 [0] => Hello World. Testing testing!

 [1] => Another day, another line.

 [2] => If the array picks up this line,

 [3] => then is it a pickup line?

 )


哪些函数用于读取文件的内容?

1/2