Php file_get_contents Function

Source: Internet
Author: User

Php file_get_contents Function

File_get_contents
("= 4.3.0, PHP 5" in PHP 4)

File_get_contents-read the entire file and convert it to a string

Description
String file_get_contents (string $ file [summary $ flag = 0 [, resource $ background [summary $ offset =-1 [summary $ maxlen =-1])
This function is similar to a file (), but the file in the string returned by file_get_contents () starts to offset a maximum of maxlen bytes at the specified time. If it fails, file_get_contents () returns FALSE.

File_get_contents () is the preferred method to read the file content as a string. It will use the support of memory ing technology if your operating system to improve performance.

Note: If you open a special URI character, such as a space, you need to encode the URI in urlencode ().


Parameters

File Name
Change the file name.

Flag
Warning
For all previous versions of PHP 6, this parameter is called use_include_path, Which is boolean. The flag parameter is only applicable to PHP 6. If you are using an old version and want to search for a file name containing a path, this parameter must be true. From PHP 6, you must use the FILE_USE_INCLUDE_PATH flag instead.

The value of the flag can be any combination of the following flags (with some limitations), the binary or (|) operator added.


Description of flag hanging
FILE_USE_INCLUDE_PATH: The search file name contains a directory. For more information, see include_path.
FILE_TEXT is from PHP 6 and the default encoding for Data Reading is UTF-8. You can specify a different encoding, establish a custom range, or change the default value to use stream_default_encoding (). This flag cannot be used for FILE_BINARY.
FILE_BINARY has this flag. The file is in read-only binary mode. This is the default setting and cannot be used for FILE_TEXT.


Background
Create stream_context_create () within the effective range of the resource (). If you do not need to use custom settings, skip this parameter to null.

Offset
Offset reading startup.

Maxlen
Read data with the maximum length.


Return Value
This function returns an error in reading data or FALSE.

Instance

For example, #1 Get and output source webpage, website

$ Homepage = file_get_contents ('HTTP: // www.example.com /');
Echo $ homepage;
?>

Example #2 Searching within the include_path

// <= PHP 5
$ File = file_get_contents ('./lele.txt', true );
//> PHP 5
$ File = file_get_contents ('./lele.txt', FILE_USE_INCLUDE_PATH );
?>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.