Wednesday 28 August 2013

PHP Function file()

Syntax

array file ( string $filename [, int $flags [, resource $context]] );

Definition and Usage

Reads an entire file into an array.

Paramters

ParameterDescription
filenamePath to the file.
flagsThe optional parameter flags can be one, or more, of the following constants:
  • FILE_USE_INCLUDE_PATH: Search for the file in the include_path.
  • FILE_IGNORE_NEW_LINES: Do not add newline at the end of each array element
  • FILE_SKIP_EMPTY_LINES: Skip empty lines
  • FILE_TEXT; The content is returned in UTF-8 encoding. You can specify a different encoding by creating a custom context. This flag cannot be used with FILE_BINARY. This flag is only available since PHP 6.
  • FILE_BINARY: The content is read as binary data. This is the default setting and cannot be used with FILE_TEXT. This flag is only available since PHP 6.
contextA context resource created with the stream_context_create() function.

Return Value

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.

Example

Following is the usage of this function:
<?php
// Get a file into an array.
$lines = file('http://www.tutorialspoint.com/');

// Loop through our array,
foreach ($lines as $line_num => $line) {
  echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line)
  echo "<br />\n";
}
?>

No comments:

Post a Comment