PHP file_get_contents() function

Definition and Usage

The file_get_contents() function reads the entire file into a string.

and file() Similarly, the difference is that file_get_contents() reads the file into a string.

The file_get_contents() function is the preferred method for reading the contents of a file into a string. If the operating system supports it, memory-mapped technology will also be used to enhance performance.

Syntax

file_get_contents(path,include_path,context,start,max_length)
Parameters Description
path Required. Specifies the file to be read.
include_path Optional. If you also want to search for files in the include_path, you can set this parameter to "1".
context

Optional. Specifies the environment of the file handle.

context is a set of options that can modify the behavior of the stream. If null is used, it is ignored.

start Optional. Specifies the position in the file to start reading from. This parameter was added in PHP 5.1.
max_length Optional. Specifies the number of bytes to read. This parameter was added in PHP 5.1.

Description

for context Parameter support was added in PHP 5.0.0.

Tips and Comments

Comment:This function can be safely used for binary objects.

Example

<?php
echo file_get_contents("test.txt");
?>

Output:

This is a test file with test text.