File Handling in PHP
When we develop a web application using PHP, quite often we need to work with external files, like reading data from a file or maybe writing user data into file etc. So it's important to know how files are handled while working on any web application.
File Handling Operations
File handling starts with creating a file, reading its content, writing into a file to appending data into an existing file and finally closing the file. Php provides pre-defined functions for all these operations, so let's start by knowing these functions.
- Create a File:
- Open a File:
- Read a File:
- Write to a File:
- Append to a File:
- Close a File:
- Delete a File:
You must be wondering that why we have specified same functions for multiple file operations, well, that is because just by changing one or more arguments, same function can be used to perform multiple operations on file.
We will learn how to use these functions along with code examples in the next tutorials.
Uses of File in Applications
Following are some of the practical use cases where we need files in our web applications.
- Oftenly data is stored in for of JSON files, amd Php code has to read the file and then display the data on web page.
- In some simple applications, no database is used, rather data is stored in files.
- In some web applications, you may have to prepare a file for user to download, in that case Php code will create a file, write data in it and then allow the user to download it.
The above usecases are just to show that very oftenly file handling is required.