In PHP, get_resource_type() returns a string that names the kind of a resource. For a file or stream opened with fopen(), it returns "stream".
A resource is a special value that points to something outside PHP, such as an open file. This function tells you what kind it is. For a resource that has already been closed, it returns "Unknown".
Syntax
Call get_resource_type() with the values you need. Start with this line.
PHP
$type = get_resource_type($resource);
Call
What it does
get_resource_type($handle)
Returns "stream" for a file or stream opened with fopen().
get_resource_type($closed)
Returns "Unknown" after the resource has been closed.
get_resource_type("text")
Throws a TypeError, because a string is not a resource.
Most beginner code only sees the type "stream". Other types come from extensions, such as a database or curl handle in older PHP versions. Check with is_resource() first if you are not sure that the value is an open resource.
Library declarations
PHP
get_resource_type(resource $resource): string
Parameters
Parameter
Description
resource
Required. The resource to inspect, for example the handle returned by fopen().
Return Value
Return Value
Description
string
The resource type name, such as "stream". It returns "Unknown" for a closed resource.
PHP Examples
Example 1 — Finding the Type of a Stream
Open an in-memory stream with fopen() and ask PHP what kind of resource it is.
Two different stream sources, php://memory and php://temp, are both of the type stream.
After fclose($a), the variable still exists, but PHP no longer treats it as an open resource.
get_resource_type() now returns Unknown, instead of throwing an error.
is_resource($a) returns false, which is the simplest way to check whether a handle is still open.
Example 3 — A Safe Describe Helper
Wrap the check in a small function so any value can be described without errors.
PHP
<?php
function describe($value) {
if (is_resource($value)) {
return "Resource of type " . get_resource_type($value);
}
return "Not an open resource: " . get_debug_type($value);
}
$handle = fopen('php://memory', 'r');
echo describe($handle), PHP_EOL;
fclose($handle);
echo describe($handle), PHP_EOL;
echo describe("text"), PHP_EOL;
Output
Resource of type stream
Not an open resource: resource (closed)
Not an open resource: string
How It Works
is_resource() is true only for open resources, so get_resource_type() is called safely.
For an open stream, the function returns Resource of type stream.
After fclose(), is_resource() is false, and get_debug_type() describes the value as resource (closed).
A string never reaches get_resource_type(), so there is no TypeError.
Frequently Asked Questions
It returns the type name of a resource as a string. For files and streams opened with fopen(), the answer is "stream".
It returns the string "Unknown". The call does not fail, so you can use it to notice that a handle was closed.
PHP 8 throws a TypeError, because the argument must be a resource. Use is_resource() first when the value might be something else.
gettype() only returns "resource" (or "resource (closed)"). get_resource_type() tells you what kind of resource it is, such as "stream".
Yes, for streams, files and some extensions. However, many extensions that used to return resources (such as curl, GD and XML parsers) now return objects instead.