PHP file_get_contents

PHPz
Release: 2024-08-29 12:35:16
Original
659 people have browsed it

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);
Copy after login

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;
Copy after login

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;
Copy after login

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;
Copy after login

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;
Copy after login

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;
Copy after login

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!

Related labels:
php
source:php
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!