how to use php file operations Explanation with example?

In this Tutorial, we will be discussed to you about the following functions related to files −
Opening a file
Closing a file
Writing a file
Reading a file

Opening and Closing Function Files

Here, fopen() function is used to open a file in PHP.
It needs the two arguments stating first the file name and another then mode in which to operate.
Files modes and purpose will be specified as one of the six options in this table.

Sr.No Purpose & Mode
1

r

it’s used to Opens the file for reading only.

Places the file pointer at the beginning of the file because of A file pointer is a pointer to structure, which contains Details about the file.

2

r+

its use to Opens the file for reading and writing.

Places the file pointer at the beginning of the file because of A file pointer is a pointer to structure, which contains Details about the file.

3

w

it uses to Opens the file for writing only.

Places the file pointer at the beginning of the file because of A file pointer is a pointer to structure, which contains Details about the file.

and shorten or truncates the file to zero length.

If files do not exist then it achieves or attempts to create a file.

4

w+

it uses to Opens the file for reading and writing only.

Places the file pointer at the beginning of the file because of A file pointer is a pointer to structure, which contains Details about the file.

and shorten or truncates the file to zero length.

If files do not exist then it achieves or attempts to create a file.

5

a

it uses to Opens the file for writing only.

Places the file pointer at the end of the file because of A file pointer is a pointer to structure, which contains Details about the file.

If files do not exist then it achieves or attempts to create a file..

6

a+

it uses to Opens the file for reading and writing only.

it’s Places the file pointer at the end of the file because of A file pointer is a pointer to structure, which contains Details about the file.

If files do not exist then it achieves or attempts to create a file.

If open a filed attempt fails then fopen returns a value of false otherwise it will return a file pointer.
which is used for writing or reading to that file.

and the opened file it is important to close it with the fclose() function.

fopen() –

In PHP File function fopen() is used to open a file. the First parameter of fopen() contains the name of the file which is to be opened and the second parameter tells about the mode in which the file needs to be opened.

fread() –

when the file is opened using fopen() then the contents of data are read using fread().
It contains two arguments
First is file pointer and second is file size in bytes.

similarly, Let’s see another example assigns the content of a text file to a variable then it will display those contents on the web page.

fwrite() –

Here, can be make a new file or text can be appended to an existing file using fwrite() function.
there are Arguments for fwrite() function first file pointer and another text that is to written to file.
It can contain optional 3rd argument where the length of text for written is specified

similarly, Let’s see another example which creates a new text file then writes a short text heading inside it.
After when will be closed file its existence is confirmed using file_exist() function which takes the file name as an argument

fclose() –

the file is closed when using fclose() function.
here, Its argument is the file which needs for be closed