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.
ADVERTISEMENT Popular Course in this category Content Marketing Mastery: Strategies for Success - Specialization | 4 Course SeriesStart Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
Here are the following examples mention below
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.
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.
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.
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.
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.
The above is the detailed content of PHP file_get_contents. For more information, please follow other related articles on the PHP Chinese website!