PHP 中的 file_get_contents() 函数

phpserver side programmingprogramming

file_get_contents() 函数将整个文件读入字符串。 file() 函数将整个文件读入数组,而 file_get_contents() 函数将整个文件读入字符串。

语法

file_get_contents(file_path, flags, context, start_offset, max_length)

参数

  • file_path − 文件的路径。

  • flags − flags 的值可以是以下标志的任意组合,并用二进制或 (|) 运算符连接。

    • FILE_USE_INCLUDE_PATH − 在包含目录中搜索文件名。

    • FILE_TEXT −如果启用了unicode语义,则读取数据的默认编码为UTF-8。此标志不能与FILE_BINARY一起使用。

    • FILE_BINARY − 使用此标志,文件以二进制模式读取。这是默认设置,不能与FILE_TEXT一起使用。

  • context − 使用stream_context_create()创建的上下文资源。

  • start_offset − 设置文件中读取的起点。

  • ma​​x_length − 要读取的数据的最大长度。默认是读取直到文件末尾。

返回

file_get_contents() 函数返回要读取的数据。失败时返回 false。

假设我们有一个文件"info.txt",内容如下。

The U.S. is a country of 50 states.

示例

<?php
   $info = file_get_contents('info.txt',FALSE, NULL, 0, 50);
   echo $info;
?>

输出

The U.S. is a country of 50 states.

让我们看另一个示例,其中我们将从网站主页读取内容。

示例

<?php
   $website = file_get_contents("https://www.qries.com");
   echo $website;
?>

输出

Sharing Knowledge

相关文章