PHP:如何读取文本文件?

时间:2020-01-09 10:42:59  来源:igfitidea点击:

如何在Linux或者UNIX下使用PHP从文本文件中读取内容?

PHP确实支持文件读取和写入。
您可以使用fopen()函数打开文件。
打开文件后,您可以使用fgets()函数读取文件。

例如,如果您想打开名为/tmp/file1.txt的文件,则以下PHP代码将帮助您读取文件。

fopen()将在r(读取)模式下正常打开。
然后while循环将使用fgets()函数打开文件,直到文件结尾:

php源代码

<html>
<head>
<title>File read demo</title>
</head>
<body>
<?php
$file = "/tmp/file1.txt";
$f = fopen($file, "r");
while ( $line = fgets($f, 1000) ) {
print $line;
}
?>
</body>
</html>

有关更多信息,请参见fopen()和fgets()。