Home  >  Article  >  Backend Development  >  php file_get_contents function_PHP tutorial

php file_get_contents function_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:00:56914browse

php file_get_contents function ​

file_get_contents
(PHP 4" = 4.3.0, PHP 5)

file_get_contents - Read the entire file and convert it into 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 file(), but file_get_contents() returns the string in the file, starting at the specified offset up to maxlen bytes. On failure, file_get_contents() will return FALSE.

file_get_contents() is the preferred way to read file contents as a string. It will use memory mapping technology, if supported by your operating system, to improve performance.

NOTE: If you open the URI with special characters such as spaces, you need to encode the URI with urlencode().


Parameters

File name
The file name is changed to .

Flag
Warning
In all versions prior to PHP 6, this parameter was called use_include_path and was a boolean. The flag parameter only works since PHP 6. If you are using an older version and wish to search for files whose names contain paths, this parameter must be true. Since PHP 6, you must use the FILE_USE_INCLUDE_PATH flag instead.

Flag values ​​can be any combination of the following flags (with some restrictions), added by the binary or ( | ) operator.


Description of flags that can be flown
FILE_USE_INCLUDE_PATH searches for directories contained in file names. See include_path for more information.
FILE_TEXT Since PHP 6, the default encoding for reading data is UTF-8. You can specify a different encoding, create a custom range, or change the default by using stream_default_encoding(). This flag cannot be used with FILE_BINARY.
FILE_BINARY With this flag, the file is in read-only binary mode. This is the default setting and cannot be used with FILE_TEXT.


Background
Establish stream_context_create() within the valid scope of the resource. If you don't need to use a custom case, you can skip this parameter and leave it null.

Offset
Offset on read start.

maxlen
Maximum length of data read.


Return value
This function returns failure to read data or FALSE.

Example

Example #1 Get and output the source page, website

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

Example #2 Searching within the include_path

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


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445473.htmlTechArticlephp file_get_contents function file_get_contents (PHP 4 = 4.3.0, PHP 5) file_get_contents - read the entire file for conversion Into a string description string file_get_contents (characters...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn