What are some alternative methods in PHP for reading and displaying the contents of a file, besides using file_get_contents()?

When reading and displaying the contents of a file in PHP, an alternative method to using file_get_contents() is to use fopen() and fread(). This method allows for more control over the reading process, such as reading the file in chunks or reading only a certain number of bytes at a time.

$filename = 'example.txt';
$handle = fopen($filename, 'r');
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        echo $line;
    }
    fclose($handle);
} else {
    echo 'Error opening the file.';
}