【php】tmpfile()で作成した一時ファイルの実ファイル名を取得する
問題
phpのtmpfile関数で作成した一時ファイルって、実ファイル名を取得する方法はないのでしょうか。

答え
案外あっさりできます。
<?php $file = tmpfile(); $path = stream_get_meta_data($file)['uri']; // 例: /tmp/php3Zqnfl
stream_get_meta_data() で、ファイルのハンドルから、実ファイルの名前が取得できます。
stream_get_meta_data() の返り値全体は以下のようになります。
array(9) {
["timed_out"]=>
bool(false)
["blocked"]=>
bool(true)
["eof"]=>
bool(false)
["wrapper_type"]=>
string(9) "plainfile"
["stream_type"]=>
string(5) "STDIO"
["mode"]=>
string(3) "r+b"
["unread_bytes"]=>
int(0)
["seekable"]=>
bool(true)
["uri"]=>
string(14) "/tmp/php3Zqnfl"
}
備考
php4では['uri']が結果に含まれておらず、パスを取得できないようです。