Read the lines in the opened file until the end of the file (EOF) is reached.
<?php $file = fopen(“test.txt”, “r”); //Output lines until EOF is reached while(! feof($file)) { $line = fgets($file); echo $line. “<br>”; } fclose($file); ?> |
The fopen() function opens a file or URL.
Note: When writing to a text file, be sure to use the correct line-ending character! Unix systems use \n, Windows systems use \r\n, and Macintosh systems use \r as the line ending character. Windows offers a translation flag (‘t’) which will translate \n to \r\n when working with the file. You can also use ‘b’ to force binary mode. To use these flags, specify either ‘b’ or ‘t’ as the last character of the mode parameter.rephrase
fopen(filename, mode, include_path, context) |
Parameter |
Description |
filename |
Essential. To open a file or URL, specify it. |
mode |
Essential. Indicates the kind of file/stream access you need.
|
include_path |
Not required. If you also wish to search for the file in the include_path (in php.ini), set this option to ‘1’. |
context |
Not required. Indicates the file handle’s context. Context is a collection of parameters that can change how a stream behaves. |
Return Value: |
When something goes wrong, FALSE, a file pointer resource, and an error occur. By using a “@” in front of the function name, you can conceal the error. |
PHP Version: |
4.3+ |
PHP Changelog: |
PHP 7.1: “e” option added |