Home >Backend Development >PHP Tutorial >PHP file_get_contents

PHP file_get_contents

PHPz
PHPzOriginal
2024-08-29 12:35:16696browse

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 Series

Start 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.

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:

PHP file_get_contents

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:

PHP file_get_contents

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:

PHP file_get_contents

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:

PHP file_get_contents

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:

PHP file_get_contents

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Local Variable in PHPNext article:Local Variable in PHP