PHP Introduction | Using Text Files | File Access with fopen
When you need more flexible handling than simply reading a file with file(), you can combine PHP’s basic file-access functions. File access follows a three-step process.
1. Open the File
$variable = fopen(file specification, mode specification);
First, call the fopen function. It opens the file specified in the argument and returns a “file pointer” that lets you access that file. Whenever you access a file, you first open it with fopen.
The important part of fopen is the second argument, the “mode specification.” This specifies how the file will be accessed. The access mode defines what kind of file handling will be done and what operations are permitted. Specify the mode with the following symbols.
Symbols for fopen Modes
| Argument | Mode | Pointer Position | If the File Exists |
|---|---|---|---|
| r | Read only | Beginning of the file | Preserve file contents |
| r+ | Read and write | Beginning of the file | Preserve file contents |
| w | Write only | Beginning of the file | Delete contents; create a new file if it does not exist |
| w+ | Read and write | Beginning of the file | Delete contents; create a new file if it does not exist |
| a | Write only | End of the file | Preserve contents; create a new file if it does not exist |
| a+ | Read and write | End of the file | Preserve contents; create a new file if it does not exist |
| x | Write only | Create a new file | Return false and emit an error if the file exists |
| x+ | Read and write | Create a new file | Return false and emit an error if the file exists |
- You can append
bortto the end of the mode argument.- b: Open in binary mode.
- t: Convert line endings for text files. Unix uses
\n, older Mac systems use only\r, and Windows uses\r\n; this mode converts\nto\r\n. It is useful when opening text files from other systems on a Windows platform.
2. Access the Data
After opening a file, the next step is to read the necessary data from it or write data to it. PHP provides functions such as fgets and fputs for this purpose. Each will be explained later.
3. Close the File
fclose(file pointer);
When data access is complete, close the file. Use the fclose function for this. Pass it the file pointer obtained from fopen.
Why do you need to open a file with fopen and close it with fclose? Because this controls access rights to the file. In general, when an application uses a file, other applications cannot open it, delete it, or freely modify it while it is in use. This indicates that “this application is using the file” and prevents access from other programs.
If several programs could access a file at the same time, the contents might be rewritten unexpectedly. To prevent the data written in the file from being damaged, the rule is that only one application can use a file at a time.
fopen and fclose do the same kind of work. When you open a file with fopen, other programs cannot freely open and rewrite it. After all processing is finished, calling fclose closes and releases the file so that other programs can use it.