php

php file_get_contents() 函数

file_get_contents() 是 PHP 内置的函数,用于 读取文件内容 或 从输入流读取数据。


1. 读取本地文件

file_get_contents() 可以读取本地文件的内容:


$content = file_get_contents("example.txt");
echo $content;

示例:如果 example.txt 内容是:

Hello, World!

则输出:

Hello, World!

特点:

适用于读取小文件(大文件建议用 fopen() + fread())。

读取整个文件内容并返回为字符串。

2. 读取远程文件(HTTP 请求)

可以用 file_get_contents() 读取网页或 API 数据:


$url = "https://api.github.com/";
$options = [
    "http" => [
        "header" => "User-Agent: PHP"
    ]
];
$context = stream_context_create($options);
$data = file_get_contents($url, false, $context);
echo $data;
这里 stream_context_create() 用于设置 HTTP 头信息,部分网站可能需要 User-Agent 以防止 403 Forbidden 错误。
3. 读取 POST 请求的原始数据
在 application/json 或 text/plain 请求中,$_POST 不能解析数据,必须用 file_get_contents("php://input"):
$json_data = file_get_contents("php://input");
$data = json_decode($json_data, true);
print_r($data);

适用于:

Content-Type: application/json

Content-Type: text/plain

application/octet-stream(二进制流)

4. 读取 php://input 处理原始请求

php://input 是 PHP 的输入流,适用于读取 POST 请求的 原始数据:



$raw_post_data = file_get_contents("php://input");
echo $raw_post_data;

为什么不用 $_POST?

$_POST 只能解析 application/x-www-form-urlencoded,无法解析 JSON。

php://input 可以读取 所有 POST 类型 的数据,包括 JSON、XML、纯文本等。

5. 读取大文件(谨慎使用)

$file_content = file_get_contents("large_file.txt");

注意:

$handle = fopen("large_file.txt", "r");while (($line = fgets($handle)) !== false) {    echo $line; }fclose($handle);

file_get_contents() 一次性加载整个文件,大文件可能导致 内存溢出。

处理大文件建议使用 fopen() 逐行读取:


$handle = fopen("large_file.txt", "r");
while (($line = fgets($handle)) !== false) {
    echo $line;
}
fclose($handle);

6. file_get_contents() 常见参数

file_get_contents(string $filename, bool $use_include_path = false, resource $context = null, int $offset = 0, int $length = null): string|false

参数 说明

$filename 需要读取的文件路径或 php://input

$use_include_path 是否在 include_path 中查找文件,默认 false

$context 可选的 stream_context(用于 HTTP 请求)

$offset 从文件的某个字节开始读取

$length 读取的最大长度(默认读取全部)

示例(读取文件的前 100 个字节):

$content = file_get_contents("example.txt", false, null, 0, 100);

7. file_get_contents() 返回值

成功时返回文件内容(字符串)。

失败时返回 false(如文件不存在)。

示例:防止 false 报错


$content = @file_get_contents("nonexistent.txt");
if ($content === false) {
    echo "文件不存在或无法读取";
}

8. file_get_contents() 与 cURL 的区别

方法 适用场景 是否支持 HTTP 请求

file_get_contents() 读取本地/远程文件,简单 HTTP 请求

cURL 示例(适用于 POST 请求)


$ch = curl_init("https://example.com/api");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(["key" => "value"]));
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

总结

用途 代码示例

读取本地文件 $content = file_get_contents("file.txt");

读取远程网页 $html = file_get_contents("https://example.com");

读取 JSON 请求 $json = file_get_contents("php://input");

从某个偏移读取文件 $content = file_get_contents("file.txt", false, null, 10, 100);

错误处理 $content = @file_get_contents("file.txt") ?: "文件不存在";

file_get_contents() 适用于小文件和简单 HTTP 请求,处理大文件或复杂请求时建议用 fopen() 或 cURL!



编辑:

阅读量:25

url链接:https://www.qozr.com/cms_php-filegetcontents-han-shu.html

Tag标签: file_get_contents() , php

同类新闻

更多新闻

Copyright © 千欧中软 版权所有 https://www.qozr.com seo | 网站建设 [渝ICP备15005074号] 渝公网安备50011802011077