file function in php


File function in PHP reads an entire file in an array. A URL can be used as a filename with this function if the fopen wrappers have been enabled.

Source:function.file.php

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

<?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/');

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
echo "Line #{$line_num} : " . htmlspecialchars($line) . "
\n";
}

// Another example, let’s get a web page into a string. See also file_get_contents().
$html = implode('', file('http://www.example.com/'));

// Using the optional flags parameter since PHP 5
$trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
?>

Related Posts: