Syntax:
file_get_contents(string $filename, bool $use_include_path = false, resource $context = null, int $offset = 0, int $length = -1)
Parameters:
- $filename: The file path or URL from which to read the contents.
- $use_include_path (optional): If set to
true
, PHP will search for the file in the include path. - $context (optional): A valid context resource created with
stream_context_create()
(used for advanced stream options). - $offset (optional): The starting point in the file from where to begin reading.
- $length (optional): The maximum number of bytes to read. Defaults to
-1
, meaning read the entire file.
Return Value:
It returns the file contents as a string on success or false
on failure.
Example 1: Reading a Local File
<?php
$filename = "example.txt";
$content = file_get_contents($filename);
if ($content !== false) {
echo "File contents: \n" . $content;
} else {
echo "Error reading the file.";
}
?>
This example reads the contents of example.txt
and prints them. If the file is not found or an error occurs, it will display an error message.
Example 2: Reading a URL
<?php
$url = "https://www.example.com";
$content = file_get_contents($url);
if ($content !== false) {
echo "Content fetched from URL: \n" . $content;
} else {
echo "Error fetching the URL.";
}
?>
This example fetches the HTML content from the specified URL and prints it.
Example 3: Using Offset and Length to Read a Portion of a File
<?php
$filename = "example.txt";
$content = file_get_contents($filename, false, null, 5, 10); // Start at offset 5, read 10 characters
if ($content !== false) {
echo "Part of file content: \n" . $content;
} else {
echo "Error reading the file.";
}
?>
In this example, the function starts reading from byte 5 and retrieves the next 10 characters.
Example 4: Handling Errors with file_get_contents()
<?php
$filename = "nonexistentfile.txt";
$content = @file_get_contents($filename);
if ($content === false) {
echo "File could not be read.";
} else {
echo $content;
}
?>
Here, the @
operator suppresses error messages, and if the file doesn't exist, file_get_contents()
will return false
.
Example 5: Using Context for Custom HTTP Headers (Fetching a URL with Headers)
<?php
$context = stream_context_create([
'http' => [
'header' => "User-Agent: PHP\r\n"
]
]);
$url = "https://www.example.com";
$content = file_get_contents($url, false, $context);
if ($content !== false) {
echo "Content fetched with custom header: \n" . $content;
} else {
echo "Error fetching the URL.";
}
?>
This example creates a stream context with custom HTTP headers and fetches content from a URL using those headers.
Key Points:
file_get_contents()
is suitable for smaller files or when you want to quickly retrieve file content.- For large files, it might be better to use other methods like
fopen()
andfread()
to avoid memory issues. - Be careful when reading files from external URLs to prevent security risks like SSRF (Server-Side Request Forgery).
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.