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 ������˵�пո񣩣�����Ҫʹ�� 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 ��װ�����ڴ˺����У� URL ����Ϊ�ļ������������ָ���ļ������ fopen()������ wapper �IJ�ͬ������μ� ֧�ֵ�Э��ͷ�װЭ����ע�����÷�������ṩ��Ԥ���������

Warning

ʹ�� SSL ʱ��Microsoft IIS ��Υ��Э�鲻����close_notify��Ǿ͹ر����ӡ�PHP ���ڵ�������β��ʱ����"SSL: Fatal Protocol Error"�� Ҫ��������⣬error_reporting Ӧ�趨Ϊ���ͼ��������������档 PHP 4.3.7 �����߰汾������ʹ�� https:// ��װ������ʱ����������� IIS ��������� �����ƾ��档��ʹ�� fsockopen() ���� ssl:// �׽���ʱ, ���������Ⲣ���ƴ˾��档

�μ�