Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

file()

Example

Load a file into an array, with each line as an element.

<?php
print_r(file(“test.txt”));
?>

Definition and Usage

The file() function reads a file into an array, where each element represents a line from the file, including the newline character.

Syntax

file(filenameflagcontext)

Parameter Values

 

Parameter

Description

filename

Mandatory. Specifies the path to the file to be read.

flag

Optional. Can include one or more of the following constants:

·         FILE_USE_INCLUDE_PATH – Search for the file in the include path specified in php.ini.

·         FILE_IGNORE_NEW_LINES – Omit the newline character at the end of each array element.

·         FILE_SKIP_EMPTY_LINES – Skip empty lines in the file.

Optional. Specifies the context for the file handle. A context is a set of options that can modify stream behavior. This can be omitted by using NULL.

context

Optional. Specifies the context for the file handle. A context is a set of options that can alter stream behavior. This parameter can be omitted by using NULL.

Technical Details

Return Value:

Returns the entire file as an array, or FALSE on failure.

PHP Version:

4.0+

Binary Safe:

Yes, in PHP 4.3