現在の言語: 日本語

戻る

file_put_contents
出力関連

file_put_contentsは、PHPでデータをファイルに書き込むための関数です。
第1引数(必須)ファイル名書き込み先のパス
第2引数(必須)文字列、配列、またはストリームリソース書き込みデータ
第3引数(任意)フラグ[FILE_APPEND]
既存の内容を消さずに、追記します。
[LOCK_EX]
書き込み中にファイルを排他ロックし、他のプロセスからの干渉を防ぎます。
[FILE_USE_INCLUDE_PATH]
include_path(ライブラリなどが置いてある標準ディレクトリ群)の中から、
指定したファイル名を探して書き込みを試みます。
※ |(パイプ)記号を使うことで、複数の設定を同時に適用できます。

[サンプル]

copy
[ケース1]
$d1=(int)$data["d1"];
$d2=(int)$data["d2"];
$lang=(int)$data["lang"];
file_put_contents('debug.log', "d1:$d1 d2:$d2 lang:$lang\n", FILE_APPEND);
[出力例]
d1:$7 d2:$2 lang:0

[ケース2]
[localhost/index.php]
equire_once("clsMain.php");
class index extends oya
{
	function test()
	{
		file_put_contents('debug.log', "index.php test()\n", FILE_APPEND);
	}
}

[localhost/common/index.php]
class oya
{
	function test2()
	{
		file_put_contents('debug.log', "oya.php test2()\n", FILE_APPEND);
	}
}
$cls = new index();
$cls->test();
$cls->test2();

[ケース3]
[localhost/index.php]
equire_once("clsMain.php");
class index extends oya
{
	function test()
	{
		file_put_contents(__DIR__ . 'debug.log', "index.php test()\n", FILE_APPEND);
	}
}

[localhost/common/index.php]
class oya
{
	function test2()
	{
		file_put_contents(__DIR__ . 'debug.log', "oya.php test2()\n", FILE_APPEND);
	}
}
$cls = new index();
$cls->test();
$cls->test2();
copy
[Case 1]
$d1=(int)$data["d1"];
$d2=(int)$data["d2"];
$lang=(int)$data["lang"];
file_put_contents('debug.log', "d1:$d1 d2:$d2 lang:$lang\n", FILE_APPEND);
[Output example]
d1:$7 d2:$2 lang:0

[Case 2]
[localhost/index.php]
equire_once("clsMain.php");
class index extends oya
{ 
	function test() 
	{ 
		file_put_contents('debug.log', "index.php test()\n", FILE_APPEND); 
	}
}

[localhost/common/index.php]
class oya
{ 
	function test2() 
	{ 
		file_put_contents('debug.log', "oya.php test2()\n", FILE_APPEND); 
	}
}
$cls = new index();
$cls->test();
$cls->test2();

[Case 3]
[localhost/index.php]
equire_once("clsMain.php");
class index extends oya
{ 
	function test() 
	{ 
		file_put_contents(__DIR__ . 'debug.log', "index.php test()\n", FILE_APPEND); 
	}
}

[localhost/common/index.php]
class oya
{ 
	function test2() 
	{ 
		file_put_contents(__DIR__ . 'debug.log', "oya.php test2()\n", FILE_APPEND); 
	}
}
$cls = new index();
$cls->test();
$cls->test2();
FILE_APPEND
[上書き]
デフォルトでは既存のファイル内容はすべて削除され、新しいデータに置き換わります。

[戻り値]
成功すると書き込まれたバイト数を返し、失敗すると false を返します。

[ディレクトリ権限]書き込み先のディレクトリに、PHPを実行するユーザーの書き込み権限(Apacheなど)が必要です。

[ケース2]
ファイルは「現在実行(アクセス)されているスクリプト」のあるディレクトリに作成されます。
そのため
localhost/index.php
を実行した場合、継承した
localhost/common/index.php
のtest2を実行したとしても
localhost直下のdebug.logに
testとtest2の実行結果が出力されます。

[ケース3]
index.php の test() を実行
__DIR__ は localhost/ (ルート) に出力します。
[作成場所]
localhost/debug.log
oya.php の test2() を実行
__DIR__ は localhost/common/ に出力します。
[作成場所]
localhost/common/debug.log

[LOCK_EX]
ファイルに書き込む際に「排他的ロック(Exclusive Lock)」をかけるためのフラグです。

[注意点]
[読み込み時の制限]
LOCK_EX は「書き込み」をブロックするものですが、
OSや設定によっては、書き込みロック中のファイルを他のプログラムが
file_get_contents などで「読み込む」ことすら制限される場合があります。

[ネットワークファイルシステム]
NFS(ネットワーク経由の共有フォルダ)など、環境によってはファイルロックが正しく動作しないことがあります。
※LANも同様です。安全に書き込みをすることを考慮する場合はデータベースを利用することなどを視野に入れてください。

[自動解除]
file_put_contents を使った場合、
関数の実行が終われば自動的にロックは解除(アンロック)されるため、
自分で解除コードを書く必要はありません。

パラメータに割り当てられているビット(数字)
FILE_USE_INCLUDE_PATH = 1
FILE_APPEND = 8
LOCK_EX = 2


戻る

著作権情報
ホームページおよびアプリ等に掲載されている情報等については、いかなる保障もいたしません。
ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、
または公開したりすることはできません。
当方は、ホームページおよびアプリ等を利用したいかなる理由によっての障害等が発生しても、
その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について
一切の責任を負わないものとします。