The file_get_contents() is an inbuilt PHP function used to read file into a string. This function uses the memory mapping technique to enhance the performance, and therefore, this function is the most preferred method to read a file into a string.
Notes:
- This function is compatible with PHP version 4.3 and above.
- This function is Binary safe, which allows this function to operate on binary file without modifying the content of the file.
Syntax
file_get_contents($path, $include_path, $context, $start, $max_length)
Parameters
Parameter | Type | Description |
$path (mandatory) | specify the location of the file you want to read | |
$include_path (Optional) | Search for the file in the include_path (in php.ini) if this parameter is set 1. | |
$context (Optional) | specifies a custom context | |
$start (Optional) | ||
$max_length (Optional) |
Return Value:
It returns the read data on success and FALSE on failure.
Example: 1)
<?PHP
#PHP Program to illustrate the working of file_get_contents Function
#Basic Program
/*
file_get_contents Function gets the complete page
of Stechies.com
*/
$homePage = file_get_contents('https://www.stechies.com');
print_r($homePage);
?>
Example 2)
<?
#PHP Program to illustrate the working of file_get_contents Function
#Basic Program
/*
file_get_contents Function gets the complete page
of Stechies.com
*/
$homePage = file_get_contents('https://www.stechies.com');
print_r($homePage);
?>