Updated April 4, 2023
Introduction to PHP file_get_contents
Whenever we want to read the contents of a file into a string in PHP, we use a function called file_get_contents() function. This function can use memory mapping techniques to improve the performance if supported by the operating system. On successfully accessing and reading the contents of the specified file, this function returns the contents of the file into a string. And if the function is not able to access the specified file or read the contents of the specified file, false is returned by this function, and the path to the location where the file to be read is present is passed as a parameter to this function.
The syntax to declare file_get_contents() function in PHP is as follows:
file_get_contents(path_to_the_file);
where path_to_the_file is the path to the file where the files are present whose contents are to be read into a string by file_get_contents() function.
Working of file_get_contents() function in PHP
- Whenever we want to read the contents of a file into a string in PHP, we use a function called file_get_contents() function.
- The path to the location where the file to be read is present is passed as a parameter to the file_get_contents() function.
- On successfully accessing and reading the contents of the specified file, the file_get_contents() function returns the contents of the file into a string.
- If the file_get_contents() function is not able to access the specified file or read the contents of the specified file, a false is returned by this function.
- The file_get_contents() function can use memory mapping techniques to improve the performance if supported by the operating system.
Examples of PHP file_get_contents
Here are the following examples mention below
Example #1
PHP program to read the contents of the file present in the location whose path is passed as a parameter to the file_get_contents() function:
Code:
#making use of file_get_contents() function by passing the path to the file where the file is present to read the contents of the file into a string called filecontents
@filecontents = file_get_contents('C:/Users/admin/Desktop/check.txt');
print "The contents of the specified file is as follows:\n";
#displaying the contents of the file
echo $filecontents;
The output of the above program is as shown in the snapshot below:
In the above program, we are making use of the file_get_contents() function by passing the path of the file where the files are present to read the contents of the file into a string called filecontents. Then the contents of the file are displayed as the output on the screen.
Example #2
PHP program to read the contents of the file present in the location whose path is passed as a parameter to the file_get_contents() function:
Code:
#making use of file_get_contents() function by passing the path to the file where the file is present to read the contents of the file into a string called filecontents
@filecontents = file_get_contents('C:/Users/admin/Desktop/EDUCBA/check1.txt’);
print "The contents of the specified file is as follows:\n";
#displaying the contents of the file
echo $filecontents;
The output of the above program is as shown in the snapshot below:
In the above program, we are making use of the file_get_contents() function by passing the path of the file where the files are present to read the contents of the file into a string called filecontents. Then the contents of the file are displayed as the output on the screen.
Example #3
PHP program to read the contents of the file present in the location whose path is passed as a parameter to the file_get_contents() function:
Code:
#making use of file_get_contents() function by passing the path to the file where the file is present to read the contents of the file into a string called filecontents
@filecontents = file_get_contents('C:/Users/admin/Desktop/check2.txt');
print "The contents of the specified file is as follows:\n";
#displaying the contents of the file
echo $filecontents;
The output of the above program is as shown in the snapshot below:
In the above program, we are making use of the file_get_contents() function by passing the path of the file where the files are present to read the contents of the file into a string called filecontents. Then the contents of the file are displayed as the output on the screen.
Example #4
PHP program to read the contents of the file present in the location whose path is passed as a parameter to the file_get_contents() function:
Code:
#making use of file_get_contents() function by passing the path to the file where the file is present to read the contents of the file into a string called filecontents
@filecontents = file_get_contents('C:/Users/admin/Desktop/check3.txt');
print "The contents of the specified file is as follows:\n";
#displaying the contents of the file
echo $filecontents;
The output of the above program is as shown in the snapshot below:
In the above program, we are making use of the file_get_contents() function by passing the path of the file where the files are present to read the contents of the file into a string called filecontents. Then the contents of the file are displayed as the output on the screen.
Example #5
PHP program to read the contents of the file present in the location whose path is passed as a parameter to the file_get_contents() function:
Code:
#making use of file_get_contents() function by passing the path to the file where the file is present to read the contents of the file into a string called filecontents
@filecontents = file_get_contents('C:/Users/admin/Desktop/check4.txt');
print "The contents of the specified file is as follows:\n";
#displaying the contents of the file
echo $filecontents;
The output of the above program is as shown in the snapshot below:
In the above program, we are making use of the file_get_contents() function by passing the path of the file where the files are present to read the contents of the file into a string called filecontents. Then the contents of the file are displayed as the output on the screen.
Recommended Articles
This is a guide to PHP file_get_contents. Here we discuss the introduction, syntax, and working of the file_get_contents() function in PHP along with different examples and their outputs. You may also have a look at the following articles to learn more –