PHP中如何读出文件内容



test4.php内容为:

“hello

world”


方式1:把文件内容直接读出到一个变量里例如$contents,类型是string,$file类型是资源句柄

<php

$file=fopen('test4.php', 'r');
$contents  =  fread ( $file ,  filesize ('test4.php'));
echo substr($contents,2,3)."\n";
fclose($file);

?>

方式2:通过判断$file是否到达文件末尾来一直持续输出,$gets()函数是逐行输出

<php

$file=fopen('test4.php', 'r');
while (!feof($file)) {
 $current_line = fgets($file);
 echo $current_line;
}
fclose($file);
?>

执行结果为:


llo
hello
world

猜你喜欢

转载自blog.csdn.net/loveuxinxin/article/details/76577268