What is fopen function PHP?
fopen() opens a file (or a URL) and returns a file handle, that you use with functions like fgets(), fwrite() and fclose(). If it fails, it returns false and shows a warning.
<?php
$handle = fopen("sample.txt", "r");
if ($handle === false) {
die("Could not open the file.");
}
echo fgets($handle); // read the first line
fclose($handle);
?>
Output:
Tutorials Class - Output Window
Line 1
The second parameter is the mode: r read, w write (erases the file), a append, x create a new file. See the full table in PHP File Handling. Always close the file with fclose().
Learn more about the similar topics:
| Tutorials |
|---|
| PHP Advanced |
| PHP File Upload |
| PHP File Handling |
| PHP File Inclusion |
| Exercises & Assignments |
|---|
| No Content Found. |