如何确定变量是否包含PHP中的文件指针?

Dar*_*ein 3 php

正如问题所述:如何检查PHP中的变量是否包含文件指针?有些喜欢is_string()is_object().

Wic*_*wok 10

您可以使用get_resource_type()- http://us3.php.net/manual/en/function.get-resource-type.php.如果函数根本不是资源,则该函数将返回FALSE.

$fp = fopen("foo", "w");
...
if(get_resource_type($fp) == 'file' || get_resource_type($fp) == 'stream') {
    //do what you want here
}
Run Code Online (Sandbox Code Playgroud)

PHP文档说上面的函数调用应该返回'file',但在我的设置中它返回'stream'.这就是我检查上述结果的原因.