Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

file_get_contents()

Example

Load the contents of a file into a string.

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

Definition and Usage

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

 

This function is the preferred method for reading file contents into a string. It may use memory mapping techniques, if supported by the server, to improve performance.

Syntax

file_get_contents(pathinclude_pathcontextstartmax_length)

Parameter Values

 

Parameter

Description

path

Mandatory. Specifies the path to the file to be read.

include_path

Optional. Set this parameter to 1 to search for the file in the include path specified in php.ini as well.

context

Optional. Specifies the context for the file handle. A context is a set of options that can alter stream behavior. This can be omitted by using NULL.

start

Optional. Specifies the position in the file to start reading from. Negative values count backward from the end of the file.

max_length

Optional. Specifies the maximum length of data to read. The default is to read until the end of the file (EOF).

Technical Details

Return Value:

Returns the entire file as a string, or FALSE on failure.

PHP Version:

4.3+

Binary Safe:

Yes, in PHP 4.3

PHP Changelog:

PHP 7.1 introduced support for negative values in the start parameter.
PHP 5.1 added the
start and max_length parameters.