ファイル全体を読み込んで配列に格納する
array file ( string $filename [, int $flags [, resource $context ]] )
〔例1〕
<?php
// ファイルの内容を配列に取り込みます。
// この例ではHTTPを通してURL上のHTMLソースを取得します。
$lines = file('http://agorian.com/help/web/php/input.html');
// 配列をループしてHTMLをHTMLソースとして表示します。
//行番号をつけるときは、echo "Line #<b>{$line_num}</b> : "
foreach ($lines as $line_num => $line) {
echo htmlspecialchars($line) . "<br />\n";
}
?>
<html>
<head>
<title>input.html</title>
</head>
<body>
入力フォームです。名前を入力してみましょう。
<form action="http://agorian.com/help/web/php/output.php" method="post">
<table border="1"> <tr>
<td>名前</td> <td>
<input type="text" name="name"></td>
<td colspan="2" align="center">
<input type="submit" value="入力"></td>
</tr> </table>
</body>
</html>
〔例2〕
<head>
<title>input.html</title>
</head>
<body>
入力フォームです。名前を入力してみましょう。
<form action="http://agorian.com/help/web/php/output.php" method="post">
<table border="1"> <tr>
<td>名前</td> <td>
<input type="text" name="name"></td>
<td colspan="2" align="center">
<input type="submit" value="入力"></td>
</tr> </table>
</body>
</html>
<?php
// 他の例として、Webページを文字列に取り込みます。file_get_contents()も参照してください。
$html = implode('', file('http://agorian.com/help/web/php/input.html'));
echo $html;
?>