PHP file_get_contents() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, file_get_contents() reads a whole file and returns its content as one string. It is the simplest way to read a text file, a JSON file or even a web page.

You give it a filename and it does everything for you: opening the file, reading it and closing it. If something goes wrong, it returns false instead of a string.

Syntax

Call file_get_contents() with the values you need. Start with this line.

PHP
$text = file_get_contents('data.txt');
CallWhat it does
file_get_contents('data.txt')Returns the whole file as a string.
file_get_contents('data.txt', false, null, 7, 10)Starts at byte 7 and reads 10 bytes.
file_get_contents('https://example.com/')Downloads a page, as long as allow_url_fopen is enabled.

The whole file is loaded into memory, so avoid it for very large files. To read from a URL, the allow_url_fopen setting must be on. A missing file returns false and raises a warning.

Library declarations
PHP
file_get_contents(string $filename, bool $use_include_path = false, ?resource $context = null, int $offset = 0, ?int $length = null): string|false

Parameters

ParameterDescription
filenameRequired. The path or URL of the file to read.
use_include_pathOptional. Set it to true to search the include_path folders as well. The default is false.
contextOptional. A stream context created with stream_context_create(), for example to set HTTP headers. Use null for the default.
offsetOptional. The byte position where reading starts. A negative value counts from the end of the file.
lengthOptional. The maximum number of bytes to read. The default null reads to the end of the file.

Return Value

Return ValueDescription
stringThe content of the file as a string. It can be an empty string for an empty file.
falseReturned when the file cannot be read.

PHP Examples

Example 1 — Read a Text File

Write a short message to a file and read it back as a string.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_fgc.txt';
file_put_contents($path, 'Hello, CodeToFun!');

$text = file_get_contents($path);

echo $text, PHP_EOL;
echo 'Length: ', strlen($text), PHP_EOL;

unlink($path);

How It Works

  1. file_put_contents() saves the message into a temporary file.
  2. file_get_contents() reads the whole file and returns it as a string.
  3. echo prints the text that was read.
  4. strlen() counts 17 characters, which matches the message.

Example 2 — Handle a Missing File

When the file does not exist, file_get_contents() returns false. Use === false so an empty file is not mistaken for an error.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_missing_fgc.txt';

$text = @file_get_contents($path);

if ($text === false) {
    echo 'Could not read the file', PHP_EOL;
} else {
    echo 'File content: ', $text, PHP_EOL;
}

How It Works

  1. The path points to a file that does not exist.
  2. file_get_contents() fails and returns false, with a warning that @ hides.
  3. === false only matches a real failure, never an empty string.
  4. The script prints a friendly message instead of crashing.

Example 3 — Read Only a Part of a File

Use the offset and length arguments to read a slice of the file instead of everything.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_fgc_part.txt';
file_put_contents($path, 'Hello, CodeToFun!');

$part = file_get_contents($path, false, null, 7, 10);

echo $part, PHP_EOL;

unlink($path);

How It Works

  1. The text Hello, takes 7 bytes, including the comma and the space.
  2. An offset of 7 makes reading start at the letter C.
  3. A length of 10 reads the ten characters CodeToFun!.
  4. The second and third arguments are false and null, which are the defaults.

Frequently Asked Questions

It reads an entire file, or a URL, and returns its content as a string. It returns false if the file cannot be read.
Use file() to get an array of lines, or open the file with fopen() and read it with fgets() in a loop. The second way uses less memory.
Yes, when allow_url_fopen is enabled in php.ini. For more control over headers and errors, many developers use cURL instead.
The file may not exist, the path may be wrong, you may lack permission, or the URL may be blocked. Check the warning message for details.
file_get_contents() gives you one string with the whole file. file() gives you an array with one item per line.

Next: file_put_contents()

file_put_contents() writes data to a file in a single call. It creates the file if it does.

file_put_contents() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful