$file_handle = fopen("myfile", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
echo $line;
}
fclose($file_handle);
-----------------------------------------------
echo file_get_contents("myfilename");
echo file_get_contents("http://127.0.0.1/");
@readfile("http://127.0.0.1/"); // prevent an error message if failes
-----------------------------------------------
<?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 #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\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);
?>
-----------------------------------------------
$array = file("myfile");
example:
; Comment
[personal information]
name = "King Arthur"
quest = To seek the holy grail
favorite color = Blue
[more stuff]
Samuel Clemens = Mark Twain
Caryn Johnson = Whoopi Goldberg
$file_array = parse_ini_file("holy_grail.ini");
print_r $file_array;
Array
(
[name] => King Arthur
[quest] => To seek the Holy Grail
[favorite color] => Blue
[Samuel Clemens] => Mark Twain
[Caryn Johnson] => Whoopi Goldberg
)
$file_array = parse_ini_file("holy_grail.ini", true);
print_r $file_array;
Array
(
[personal information] => Array
(
[name] => King Arthur
[quest] => To seek the Holy Grail
[favorite color] => Blue
)
[more stuff] => Array
(
[Samuel Clemens] => Mark Twain
[Caryn Johnson] => Whoopi Goldberg
)
)