Description

Parameters

filename

Path to the file.

Tip

A URL can be used as a
filename with this function if the fopen wrappers have been enabled.
See fopen() for more details on how to specify the
filename. See the Supported Protocols and Wrappers for links to information
about what abilities the various wrappers have, notes on their usage,
and information on any predefined variables they may
provide.

flags

The optional parameter flags can be one, or
more, of the following constants:

Note: Context support was added
with PHP 5.0.0. For a description of contexts, refer to
Streams.

Return Values

Returns the file in an array. Each element of the array corresponds to a
line in the file, with the newline still attached. Upon failure,
file() returns FALSE.

Note:

Each line in the resulting array will include the line ending, unless
FILE_IGNORE_NEW_LINES is used, so you still need to
use rtrim() if you do not want the line ending
present.

Note: If PHP is not properly recognizing
the line endings when reading files either on or created by a Macintosh
computer, enabling the
auto_detect_line_endings
run-time configuration option may help resolve the problem.

Changelog

Version

Description

4.3.0

file() became binary safe

Examples

Example #1 file() example

<?php// Get a file into an array. In this example we'll go through HTTP to get// the HTML source of a URL.$lines = file('http://www.example.com/');

Notes

Warning

When using SSL, Microsoft IIS
will violate the protocol by closing the connection without sending a
close_notify indicator. PHP will report this as "SSL: Fatal
Protocol Error" when you reach the end of the data. To work around this, the
value of error_reporting should be
lowered to a level that does not include warnings.
PHP 4.3.7 and higher can detect buggy IIS server software when you open
the stream using the https:// wrapper and will suppress the
warning. When using fsockopen() to create an
ssl:// socket, the developer is responsible for detecting
and suppressing this warning.

If the file you are reading is in CSV format do not use file(), use fgetcsv(). file() will split the file by each newline that it finds, even newlines that appear within a field (i.e. within quotations).

To write all the lines of the file in other words to read the file line by line you can write the code like this:<?php$names=file('name.txt');// To check the number of lines echo count($names).'<br>';foreach($names as $name){ echo $name.'<br>';}?>

this example is so basic to understand how it's working. I hope it will help many beginners.

("file()'s problem with UTF-16" is wrong. This is updated.The former may miss the last line of the string.)

file() seems to have a problem in handling UTF-16 with or without BOM.

file() is likely to think "\n"=LF (0A) as a line-ending.So, not only "000A" but also "010A, 020A,...,FE0A, FF0A,..."are regarded as line-endings.

Moreover, file() causes a serious problem in UTF-16LE.file() loses first "0A" (the first half of "0A00")!And the next line begins with "00" (the rest of "0A00").So lines after the first "0A" are totally different.

file() has a strange behaviour when reading file with both \n and \r as line delimitator (DOS files), since it will return an array with every single line but with just a \n in the end. It seems like \r just disappears.

This is happening with PHP 4.0.4 for OS/2. Don't know about the Windows version.

If you're getting "failed to open stream: Permission denied" when trying to use either file() or fopen() to access files on another server. Check your host doesn't have any firewall restrictions in-place which prevent outbound connections. This is the case with my host Aplus.net