In PHP, you can open and close a file using the following functions:
- fopen(): This function is used to open a file. It takes two arguments, the file name and the mode in which the file should be opened. The mode can be "r" for read-only, "w" for write-only (creates a new file or overwrites an existing one), "a" for append (opens an existing file and moves the pointer to the end of the file), and "x" for exclusive creation (creates a new file but returns an error if the file already exists).
Here is an example of opening a file in read mode:
$file = fopen("example.txt", "r");
- fclose(): This function is used to close a file. It takes one argument, the file handle returned by the fopen() function.
Here is an example of closing a file:
It is important to always close a file after you are done with it to release the system resources used by the file.