PHP Advanced File Operations
Advanced file operations give you fine-grained control over file handling. Perfect for large files, streaming, and precise file manipulation!
Output
Click Run to execute your code
File Modes
| Mode | Description |
|---|---|
r |
Read only |
w |
Write only (truncates) |
a |
Append only |
r+ |
Read and write |
w+ |
Read and write (truncates) |
a+ |
Read and append |
Reading with fopen
<?php
$handle = fopen('example.txt', 'r');
while (($line = fgets($handle)) !== false) {
echo $line;
}
fclose($handle);
?>
Writing with fopen
<?php
$handle = fopen('output.txt', 'w');
fwrite($handle, "Line 1\n");
fwrite($handle, "Line 2\n");
fclose($handle);
?>
Summary
- fopen(): Open file with mode
- fread(): Read bytes
- fwrite(): Write data
- fclose(): Always close files
What's Next?
Next, learn about Directory Operations - creating and managing folders!
Enjoying these tutorials?