Backend Development 6 min read

Using PHP file_get_contents() to Read Files and URLs

This article explains the PHP file_get_contents() function, its syntax, parameters, return values, and provides practical examples for reading local files and remote URLs, as well as further processing techniques such as content replacement and writing to new files.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP file_get_contents() to Read Files and URLs

In PHP, the file_get_contents() function is a versatile tool that allows you to read the contents of a file or a remote URL with ease.

Basic Syntax

<code>string file_get_contents ( string $filename [, bool $use_include_path = FALSE [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )</code>

Parameter Description

$filename : Path to the file, which can be a local path or a remote URL.

$use_include_path : Optional, defaults to FALSE. When TRUE, the function searches for the file in the include_path .

$context : Optional stream context, created with stream_context_create() .

$offset : Optional offset where reading starts.

$maxlen : Optional maximum length of data to read.

Return Value

The function returns the read content on success, or FALSE on failure.

Example 1: Reading a Local File

The following code demonstrates how to read the contents of a local file using file_get_contents() :

<code>&lt;?php
$filename = 'test.txt';
$content = file_get_contents($filename);

if ($content !== false) {
    echo "File content:<br/>" . $content;
} else {
    echo "Unable to read file content.";
}
?&gt;</code>

If the file is read successfully, its contents are output; otherwise an error message is shown.

Example 2: Reading a Remote URL

The following code shows how to fetch the content of a remote URL:

<code>&lt;?php
$url = 'http://www.example.com';
$content = file_get_contents($url);

if ($content !== false) {
    echo "URL content:<br/>" . $content;
} else {
    echo "Unable to fetch URL content.";
}
?&gt;</code>

Successful execution prints the remote page content; failure results in an error message.

Further Processing

After obtaining content with file_get_contents() , you can manipulate it using other functions. For example, you can replace strings and write the modified content to a new file:

<code>&lt;?php
$filename = 'test.txt';
// Read file content
$content = file_get_contents($filename);

if ($content !== false) {
    // Replace 'old' with 'new'
    $newContent = str_replace('old', 'new', $content);
    // Write new content to a new file
    $newFilename = 'new_test.txt';
    file_put_contents($newFilename, $newContent);
    echo "New file created and written with new content.";
} else {
    echo "Unable to read file content.";
}
?&gt;</code>

This example reads test.txt , replaces occurrences of old with new , and saves the result to new_test.txt .

Summary

By using PHP's file_get_contents() function, you can easily read data from local files or remote URLs, then further process that data as needed—whether by modifying, replacing, or writing it to other files.

PHP Learning Recommendations

Vue3+Laravel8+Uniapp Beginner to Advanced Development Tutorial

Vue3+TP6+API Social E‑commerce System Development Course

Swoole From Beginner to Master Course

Workerman+TP6 Real‑time Chat System Limited‑time Offer

Backend Developmentcode examplesfile handlingfile_get_contentsphp tutorial
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.