PHP file_put_contents()関数
❮PHPファイルシステムリファレンス例
ファイルにデータを書き込む:
<?php
echo file_put_contents("test.txt","Hello World. Testing!");
?>
上記のコードの出力は次のようになります。
21
定義と使用法
file_put_contents()は、データをファイルに書き込みます。
この関数は、ファイルにアクセスするときに次のルールに従います。
- FILE_USE_INCLUDE_PATHが設定されている場合は、ファイル名のコピーのインクルードパスを確認してください
- ファイルが存在しない場合は作成します
- ファイルを開く
- LOCK_EXが設定されている場合は、ファイルをロックします
- FILE_APPENDが設定されている場合は、ファイルの最後に移動します。それ以外の場合は、ファイルの内容をクリアします
- データをファイルに書き込みます
- ファイルを閉じて、ロックを解除します
注:ファイルの既存のコンテンツが削除されないようにするには、FILE_APPENDを使用してください。
構文
file_put_contents(filename, data, mode, context)
パラメータ値
Parameter | Description |
---|---|
filename | Required. Specifies the path to the file to write to. If the file does not exist, this function will create one |
data | Required. The data to write to the file. Can be a string, array, or a data stream |
mode | Optional. Specifies how to open/write to the file. Possible values:
|
context | Optional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream. |
技術的な詳細
戻り値: | 成功した場合はファイルに書き込まれたバイト数、失敗した場合はFALSE |
---|---|
PHPバージョン: | 5.0以降 |
バイナリセーフ: | はい |
PHP変更ログ: | PHP 5.1-LOCK_EXのサポートと、データパラメーターにストリームリソースを渡す機能が追加されました |
❮PHPファイルシステムリファレンス