How to read the text file using PHP

How to read the text file using PHP

  • 14 July, 2018
  • Renish Khunt
  • PHP
  • 53

How to read the text file using PHP. Many times we need to store some data in the text file and read the text file. So, today we are learning how we can read the text file.

Let’s see it’s too easy to read the text file using PHP like below.

$file_handle = fopen("pathtofile.txt", "r");
$content = "";
while (!feof($file_handle)) {
    $content[] = fgets($file_handle);
}
fclose($file_handle);

echo '<pre>';
print_r($content);
exit;

Just set the text file path that you want to read.