関数 fwrite

関数 fwrite は、fopen で開いたファイルにデータを書き込みます。第一引数にはファイルポインタ、第二引数には書き込む文字列、第三引数(オプション)には書き込む最大バイト数を指定します。

構文

phpfwrite(resource, string, [length]);

例

ファイルに文字列を書き込む例:

php<?php
  $file = fopen('test.txt', 'w');
  fwrite($file, 'text');
  fclose($file);
?>

例

最初の 2 文字だけを書き込む例:

php<?php
  $file = fopen('test.txt', 'w');
  fwrite($file, 'text', 2);
  fclose($file);
?>

例

ファイルの末尾にテキストを追加する例:

php<?php
  $file = fopen('test.txt', 'a');
  fwrite($file, '!');
  fclose($file);
?>

関連項目