42. Introduction to File System Operations
PHP is often used to interact with the server's filesystem, managing files, logs, and configuration data.
1. Reading Files (file_get_contents)
The simplest way to read an entire file into a string.
php
2. Writing to Files (file_put_contents)
Writes a string to a file. If the file doesn't exist, it is created. By default, it overwrites the content.
php
3. Advanced File Handling (Opening Streams)
For large files or precise control, we use fopen(), fwrite(), and fclose().
| Function | Description |
|---|---|
fopen($file, $mode) | Opens the file stream in a specific mode (w=write, a=append, r=read). |
fwrite($handle, $string) | Writes content to the open file. |
fgets($handle) | Reads a single line from the file. |
fclose($handle) | Closes the file stream, releasing server resources. |
php