file_get_contents — 将整个文件读入一个字符串

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

file() 一样,只除了 file_get_contents() 把文件读入一个字符串。将在参数 offset 所指定的位置开始读取长度为 maxlen 的内容。如果失败, file_get_contents() 将返回 FALSE

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

Note:

如果要打开有特殊字符的 URL (比如说有空格),就需要使用 urlencode() 进行 URL 编码。

参数

filename

要读取的文件的名称。

use_include_path

Note:

As of PHP 5 the FILE_USE_INCLUDE_PATH can be used to trigger include path search.

context

A valid context resource created with stream_context_create() . 如果你不需要自定义 context,可以用 NULL 来忽略。

offset

The offset where the reading starts on the original stream.

Seeking (offset) is not supported with remote files. Attempting to seek on non-local files may work with small offsets, but this is unpredictable because it works on the buffered stream.

maxlen

Maximum length of data read. The default is to read until end of file is reached. Note that this parameter is applied to the stream processed by the filters.

 

返回值

The function returns the read data 或者在失败时返回 FALSE .

 

<?php
$homepage  file_get_contents ( 'http://www.example.com/' );
echo  $homepage ;
?>

 

<?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 );
?>

 

 项目实例:

$returnData = file_get_contents('php://input');

 if ( empty( $data ) ){
   $this -> data['result'] = '审核成功';
   _logger( $this -> data['result'] );
   http_response( $this->data );
}

 

 

posted @ 2018-04-24 11:25  北月南辰  阅读(321)  评论(0)    收藏  举报