In PHP there is a read file() function defined for reading a file.
1. fopen() function is used to open a file. The fopen() function defined with two parameters.
The first parameter in fopen() function is the name of the specified file and the second parameter is the mode for the particular file either read (r) or write (w).
Write mode modify the content of the existing file or it can be used to create the file for the same name if the file does not exist.
- The (a) mode is used to write the content at the last of the page. The existing file is not deleted. The content can be added from last and the new file is created if the file does not exist.
- The (x) mode used to write the file but if the file does not exist then it returns false as an error.
- The (r+) command is used to read/write mode.
- The (w+) mode is used for write/ read mode and through this content can be modified or deleted. read() function is used to read from an open file.
2. fclose() function is used for closing an open file. For the good developer, it is always recommended that the file must close first after completing the work on the same file.
If we want to read a single line from a file then we use fgets() function.
After calling fget() function, the pointer points to the next line and the process must go on.
The feof() function plays the role when the end of file is reached. It plays an important role in looping.
The fgetc() function is used to read the character from a file.
After calling the fgetc() function for one character then it points to the next character.
PHP is a very easy language and has very many useful functions. By using these functions we can be handling the PHP file easily and efficiently. Therefore File handling in PHP is very beneficial and necessary.
This article is contributed by Aayushi. If you like Sharecodepoint and would like to contribute, you can also write an article using email@example.com or mail your article to sharecodepoint@gmail. See your article appearing on the sharecodepoint main page and help another programmer.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.