file_get_contents()
file_get_contents() — Reads entire file into a string.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
This function is similar to file(), except that file_get_contents() returns the file in a string, starting at the specified offset up to length bytes. On failure, file_get_contents() will return false.
Signature
file_get_contents(string $filename, bool $use_include_path = false, ?resource $context = null, int $offset = 0, ?int $length = null): string|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $filename | string | Name of the file to read. |
| $use_include_path | bool | The FILE_USE_INCLUDE_PATH constant can be used to trigger include path search. This is not possible if strict typing is enabled, since FILE_USE_INCLUDE_PATH is an int. Use true instead. |
| $context | ?resource | A valid context resource created with stream_context_create(). If you don't need to use a custom context, you can skip this parameter by null. |
| $offset | int | The offset where the reading starts on the original stream. Negative offsets count from the end of the stream. |
| $length | ?int | Maximum length of data read. The default is to read until end of file is reached. Note that this parameter is applied to the stream processed by the filters. |
Return value
The function returns the read data or false on failure. This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
Example
A runnable snippet:
$homepage = file_get_contents('http://www.example.com/'); echo $homepage;
Changelog
| Version | Change |
|---|---|
| 8.0.0 | length is nullable now. |
| 7.1.0 | Support for negative offsets has been added. |
See also
file(), fgets(), fread(), readfile(), file_put_contents(), stream_get_contents(), stream_context_create()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page file_get_contents() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.