read file in php

To read a file in PHP, you first need to open the file using the fopen function. This function takes two arguments: the path to the file and the mode in which the file should be opened (read, write, append, etc.).

Once you have opened the file, you can read its contents using the fread function. This function takes two arguments: the file handle (returned by fopen) and the number of bytes to read from the file.

After you have finished reading the file, you should close the file using the fclose function.

Here's an example code that reads a file line by line:

main.php
$file = fopen("example.txt", "r");

while(!feof($file)) {
  $line = fgets($file);
  echo $line . "<br>";
}

fclose($file);
123 chars
9 lines

In this example, we open a file example.txt in read mode using fopen. We then loop through the contents of the file using fgets, which reads a single line from the file. Finally, we close the file using fclose.

gistlibby LogSnag