php file_get_contents函数怎么用-LMLPHP

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);
?>
登录后复制

以上就是php file_get_contents函数怎么用的详细内容,更多请关注Work网其它相关文章!

09-08 07:34