读取data文件里的数据

<?php

header(\"Content-type: text/html; charset=utf-8\");
 
//read data
$f = fopen(\'data\',\'r\');
$content = fgets($f);
echo $content;
fclose($f);

 

如果有多行数据该怎么读取?

方法一 while:

<?php

header(\"Content-type: text/html; charset=utf-8\");
$f = fopen(\'data\',\'r\');
//读取多行数据 while
while(!feof($f)){//feof() 函数检测是否已到达文件末尾
  $content = fgets($f);
  echo $content;
}
fclose($f);

 

方法二 file_get_contents():

echo file_get_contents(\'data\');
收藏 打印