file_get_contents

(PHP 4 >= 4.3.0, PHP 5, PHP 7)

file_get_contents锟斤拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟斤拷一锟斤拷锟街凤拷锟斤拷

说锟斤拷

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

锟斤拷 file() 一锟斤拷锟斤拷只锟斤拷锟斤拷 file_get_contents() 锟斤拷锟侥硷拷锟斤拷锟斤拷一锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟节诧拷锟斤拷 offset 锟斤拷指锟斤拷锟斤拷位锟矫匡拷始锟斤拷取锟斤拷锟斤拷为 maxlen 锟斤拷锟斤拷锟捷★拷锟斤拷锟绞э拷埽锟�file_get_contents() 锟斤拷锟斤拷锟斤拷 FALSE锟斤拷

file_get_contents() 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟斤拷锟捷讹拷锟诫到一锟斤拷锟街凤拷锟斤拷锟叫碉拷锟斤拷选锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟较低持э拷只锟斤拷锟绞癸拷锟斤拷诖锟接筹拷浼硷拷锟斤拷锟斤拷锟角匡拷锟斤拷堋锟�

Note:

锟斤拷锟揭拷锟斤拷锟斤拷锟斤拷锟斤拷址锟斤拷锟� URL 锟斤拷锟斤拷锟斤拷说锟叫空格)o拷锟斤拷锟斤拷要使锟斤拷 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.

锟斤拷锟斤拷锟届常

An E_WARNING level error is generated if either maxlength is less than zero, or if seeking to the specified offset in the stream fails.

锟斤拷锟斤拷

Example #1 Get and output the source of the homepage of a website

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

Example #2 Searching within the include_path

<?php
// <= PHP 5
$file file_get_contents('./people.txt'true);
// > PHP 5
$file file_get_contents('./people.txt'FILE_USE_INCLUDE_PATH);
?>

Example #3 Reading a section of a file

<?php
// Read 14 characters starting from the 21st character
$section file_get_contents('./people.txt'NULLNULL2014);
var_dump($section);
?>

锟斤拷锟斤拷锟斤拷锟教碉拷锟斤拷锟斤拷锟斤拷锟斤拷冢锟�

string(14) "lle Bjori Ro" 

Example #4 Using stream contexts

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

锟斤拷锟斤拷锟斤拷志

锟芥本 说锟斤拷
5.1.0 Added the offset and maxlen parameters.
5.0.0 Added context support.

注锟斤拷

Note: 锟剿猴拷锟斤拷锟缴帮拷全锟斤拷锟节讹拷锟斤拷锟狡讹拷锟斤拷

Tip

锟斤拷锟斤拷锟斤拷锟斤拷fopen 锟斤拷装锟斤拷锟斤拷锟节此猴拷锟斤拷锟叫o拷 URL 锟斤拷锟斤拷为锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街革拷锟斤拷募锟斤拷锟斤拷锟斤拷 fopen()锟斤拷锟斤拷锟斤拷 wapper 锟侥诧拷同锟斤拷锟斤拷锟斤拷渭锟� 支锟街碉拷协锟斤拷头锟阶靶拷锟�锟斤拷注锟斤拷锟斤拷锟矫凤拷锟斤拷锟斤拷锟斤拷峁╋拷锟皆わ拷锟斤拷锟斤拷锟斤拷锟斤拷

Warning

使锟斤拷 SSL 时锟斤拷Microsoft IIS 锟斤拷违锟斤拷协锟介不锟斤拷锟斤拷close_notify锟斤拷蔷凸乇锟斤拷锟斤拷印锟絇HP 锟斤拷锟节碉拷锟斤拷锟斤拷锟斤拷尾锟斤拷时锟斤拷锟斤拷"SSL: Fatal Protocol Error"锟斤拷 要锟斤拷锟斤拷锟斤拷锟斤拷猓�error_reporting 应锟借定为锟斤拷锟酵硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟芥。 PHP 4.3.7 锟斤拷锟斤拷锟竭版本锟斤拷锟斤拷锟斤拷使锟斤拷 https:// 锟斤拷装锟斤拷锟斤拷锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� IIS 锟斤拷锟斤拷锟斤拷锟斤拷锟� 锟斤拷锟斤拷锟狡撅拷锟芥。锟斤拷使锟斤拷 fsockopen() 锟斤拷锟斤拷 ssl:// 锟阶斤拷锟斤拷时, 锟斤拷锟斤拷锟斤拷锟斤拷锟解并锟斤拷锟狡此撅拷锟芥。

锟轿硷拷

  • file() - 锟斤拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷
  • fgets() - 锟斤拷锟侥硷拷指锟斤拷锟叫讹拷取一锟斤拷
  • fread() - 锟斤拷取锟侥硷拷锟斤拷锟缴帮拷全锟斤拷锟节讹拷锟斤拷锟斤拷锟侥硷拷锟斤拷
  • readfile() - 锟斤拷锟斤拷募锟�
  • file_put_contents() - 锟斤拷一锟斤拷锟街凤拷锟斤拷写锟斤拷锟侥硷拷
  • stream_get_contents() - 锟斤拷取锟斤拷源锟斤拷锟斤拷一锟斤拷锟街凤拷锟斤拷
  • stream_context_create() - 锟斤拷锟斤拷锟斤拷源锟斤拷锟斤拷锟斤拷锟斤拷
  • $http_response_header