艾Q网

标题: PHP如何打开并读取文件 [打印本页]

作者: admin    时间: 2023-12-7 20:40
标题: PHP如何打开并读取文件
逐行读取文件
fgets() 函数用于从文件中逐行读取文件。

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

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

<?php
$file = fopen("welcome.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
  {
  echo fgets($file). "<br />";
  }
fclose($file);
?>

逐字符读取文件
fgetc() 函数用于从文件逐字符地读取文件。

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

例子
下面的例子逐字符地读取文件,直到文件末端为止:

<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
while (!feof($file))
  {
  echo fgetc($file);
  }
fclose($file);
?>






欢迎光临 艾Q网 (http://js.iqi123.com/bbs/) Powered by Discuz! X3.4