Google News
logo
PHP Program to Use fopen(), fread(), and fclose() to open, read, and close a file
To open, read, and close a file using PHP, we can use the following functions:

1. `fopen()` - Used to open a file and returns a file pointer resource
2. `fread()` - Used to read content from the file
3. `fclose()` - Used to close the file and release resources

Here's an example program that demonstrates how to open, read, and close a file using these functions :
Program :
<?php
$file = fopen("example.txt", "r") or die("Unable to open file!"); // open the file in read mode

$content = fread($file, filesize("example.txt")); // read the content from the file

echo $content; // display the content of the file

fclose($file); // close the file
?>
In the above program, we opened the file "example.txt" in read mode using the `fopen()` function. Then we read the content of the file using the `fread()` function and stored it in the variable `$content`.

Finally, we displayed the content of the file using `echo` statement and closed the file using the `fclose()` function.