PHP file() Function

PHP file Function


Definition :

The PHP file() function reads a file into an array. Each element of the array corresponds to a line in the file, with the newline character still attached.

Syntax

    file(filenameflagcontext)

Parameter 
Parameter Description
filename Required. Specifies the path to the file to read
flag Optional. Can be one or more of the following constants:
  • FILE_USE_INCLUDE_PATH - Search for the file in the include_path (in php.ini)
  • FILE_IGNORE_NEW_LINES - Skip the newline at the end of each array element
  • FILE_SKIP_EMPTY_LINES - Skip empty lines in the file
context Optional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream. Can be skipped by using NULL.

Given below example Read "demo.txt" file into an array:

 Output :

Array
(
    [0] => This is the demo file

    [1] => this id the second line.

    [2] => this is the third line.

)