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.
Interview Questions & Answers
What is include() function in PHP?
Can we include a file two times in any PHP page?
Which is the PHP function to delete a file from server?
What is fopen function PHP?
What is the difference between unlink() and unset() in PHP?
How to check if a file exists in PHP?
How to get the size of a file in PHP?