php file_get_contents 中文,php file_get_contents函数怎么用

64fe1d93f2dd6ce300399dc6c8a1dd52.png

php file_get_contents函数怎么用?

作用:把整个文件读入一个字符串中。

语法:file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] ) : string

用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。

参数:filename要读取的文件的名称。

use_include_path在PHP 5中,FILE_USE_INCLUDE_PATH可以用于触发包含路径搜索。

context使用stream_context_create()创建的有效上下文资源。

如果你不需要自定义 context,可以用 NULL 来忽略。

offset读取在原始流上开始的偏移量。

远程文件不支持查找(偏移量)。尝试在非本地文件上查找可能会使用较小的偏移量,但这是不可预测的,因为它在缓冲流上工作。

maxlen读取数据的最大长度。默认的读取方式是读取到文件的末尾。请注意,此参数应用于筛选器处理的流。

返回值:

函数返回读取的数据或者在失败时返回 FALSE。

php file_get_contents()函数使用示例

获取网站主页<?php

$homepage = file_get_contents('http://www.example.com/');

echo $homepage;

?>

读取文件的一个部分<?php

// Read 14 characters starting from the 21st character

$section = file_get_contents('./people.txt', NULL, NULL, 20, 14);

var_dump($section);

?>

在包含路径中搜索<?php

// <= PHP 5

$file = file_get_contents('./people.txt', true);

// > PHP 5

$file = file_get_contents('./people.txt', FILE_USE_INCLUDE_PATH);

?>

使用上下文<?php

// Create a stream

$opts = array(

'http'=>array(

'method'=>"GET",

'header'=>"Accept-language: en\r\n" .

"Cookie: foo=bar\r\n"

)

);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above

$file = file_get_contents('http://www.example.com/', false, $context);

?>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值