PHP readfile 和 file_get_contents 函数


'readfile' 函数是 PHP 中的一个内置函数,该函数直接将文件读取到输出缓冲区中。函数的参数是文件名。在成功读取数据的情况下,该函数返回读取字节数。否则,返回 FALSE −

示例

 在线演示

<?php
// writing file contents on the output
// buffer using readfile() function
$myfile = @readfile("gfg.txt");
if (!$myfile) {
   print "Sorry, the file could not be opened";
}
?>

输出

这会输出如下内容 −

Sorry, the file could not be opened

'file_get_contents' 函数是 PHP 中的一个内置函数,该函数将文件加载到内存中,且只有在调用 echo 函数时才会显示内容。在这个阶段,数据会从内存复制到输出缓冲区,然后显示。该函数利用内存映射技术,使其成为读取文件内容的有效方式。

需要读取的文件路径作为参数传递。如果成功,该函数会返回从文件中读取的数据作为输出;否则,返回 FALSE −

<?php
// reading 36 bytes startig from
// the 0th character from gfg.txt
$text = file_get_contents(‘text_file_name.txt', FALSE, NULL, 0, 36);
echo $text;
?>

更新于:2020 年 4 月 9 日

858 次浏览

职业生涯起航

完成课程即可获取认证

开始
广告
© . All rights reserved.