ZetCode

PHP file_get_contents Function

last modified April 3, 2025

The PHP file_get_contents function reads entire file contents into a string. It's a simple way to read files without manual file handling.

Basic Definition

The file_get_contents function reads a file and returns its contents as a string. It can read local files and URLs when allow_url_fopen is enabled.

Syntax: file_get_contents(string $filename, bool $use_include_path = false, resource $context = null, int $offset = 0, int $maxlen = null): string|false.

Basic file_get_contents Example

This shows the simplest usage of file_get_contents to read a file.

basic_file_get_contents.php
<?php

declare(strict_types=1);

$content = file_get_contents('data.txt');

if ($content !== false) {
    echo $content;
} else {
    echo "Failed to read file";
}

This reads the entire contents of 'data.txt' into a string. Always check the return value as it returns false on failure.

Reading Remote Content

file_get_contents can read remote files when allow_url_fopen is on.

remote_content.php
<?php

declare(strict_types=1);

$url = 'https://example.com/data.json';
$content = file_get_contents($url);

if ($content !== false) {
    $data = json_decode($content, true);
    print_r($data);
} else {
    echo "Failed to fetch remote content";
}

This fetches JSON data from a remote URL and decodes it. Note that remote requests may fail due to network issues or server restrictions.

Using Context Options

Context options allow customizing the file reading behavior.

context_options.php
<?php

declare(strict_types=1);

$opts = [
    'http' => [
        'method' => 'GET',
        'header' => "User-Agent: MyCustomAgent\r\n"
    ]
];

$context = stream_context_create($opts);
$content = file_get_contents('https://example.com', false, $context);

if ($content !== false) {
    echo substr($content, 0, 200);
}

This sets a custom User-Agent header for the HTTP request. Context options are useful for adding headers, timeouts, or other stream behaviors.

Reading Partial Content

The function can read a portion of a file using offset and maxlen parameters.

partial_content.php
<?php

declare(strict_types=1);

$filename = 'large_file.log';
$content = file_get_contents($filename, false, null, 100, 50);

if ($content !== false) {
    echo "Read 50 bytes starting at position 100:\n";
    echo $content;
}

This reads 50 bytes starting from position 100 in the file. Partial reading is useful for large files where you only need specific portions.

Error Handling

Proper error handling is essential when working with file operations.

error_handling.php
<?php

declare(strict_types=1);

$filename = 'nonexistent.txt';

try {
    $content = file_get_contents($filename);
    
    if ($content === false) {
        throw new RuntimeException("Failed to read file");
    }
    
    echo $content;
} catch (RuntimeException $e) {
    error_log($e->getMessage());
    echo "Error: " . $e->getMessage();
}

This demonstrates proper error handling with exceptions. Always handle potential failures when working with file operations.

Best Practices

Source

PHP file_get_contents Documentation

This tutorial covered the PHP file_get_contents function with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP Filesystem Functions.