file

file -- ファイル全体を読み込んで配列に格納する

説明

array file(string filename, int [use_include_path]);

readfile()と同じですが、file() はファイルを 配列に入れて返すところだけが異なります。 配列の各要素は、ファイルの各行に対応します。改行記号はついたままと なります。

オプションの2番目の引数を使用して、これに "1" を設定する ことにより、include_path のファイルの検索も行うことができます。

  1 
  2 <?php
  3 // Webページを配列として取得し、出力します。
  4 $fcontents = file( 'http://www.php.net' );
  5 while ( list( $line_num, $line ) = each( $fcontents ) ) {
  6    echo "<b>Line $line_num:</b> " . htmlspecialchars( $line ) . "<br>\n";
  7 }
  8 
  9 // Webページを文字列として取得します。
 10 $fcontents = join( '', file( 'http://www.php.net' ) );
 11 ?>
 12       

readfile(), fopen(), popen() も参照下さい。