What are the potential pitfalls of using file() function in PHP and how can they be addressed?

The potential pitfalls of using the file() function in PHP include the fact that it reads the entire file into an array, which can consume a lot of memory for large files. To address this issue, it's better to use fopen() and fread() functions to read files line by line or in chunks.

// Open file using fopen and read line by line
$filename = "example.txt";
$file = fopen($filename, "r") or die("Unable to open file!");

while(!feof($file)) {
  $line = fgets($file);
  echo $line;
}

fclose($file);