The fopen($fileName, “r”) function allows developers to open a file and read its content. The function returns a file handle. The second argument “r” instructs PHP to open the file for reading. Once the file is open, other functions to read data from the file through the file handle can be used.
Example code on how to use fopen() for reading:
<?php $file = fopen("/tmp/file.txt", "r"); print("Type of file handle: " . gettype($file) . "\n"); print("The first line from the file handle: " . fgets($file)); fclose($file); ?>
This script will print:
Type of file handle: resource
The first line from the file handle: Welcome to w3mentor.com php tutorials
The call to fclose() is used to close the opened file to release the resources allocated.