PHP get_resource_type() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

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);
CallWhat 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

ParameterDescription
resourceRequired. The resource to inspect, for example the handle returned by fopen().

Return Value

Return ValueDescription
stringThe 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.

PHP
<?php
$handle = fopen('php://memory', 'r');

echo get_resource_type($handle), PHP_EOL;

fclose($handle);

How It Works

  1. fopen('php://memory', 'r') returns a resource that points to a memory stream.
  2. The resource is stored in $handle.
  3. get_resource_type($handle) returns the text stream.
  4. fclose() closes the handle when we are done, which is good practice.

Example 2 — Open vs Closed Resources

The type changes to Unknown as soon as the resource is closed.

PHP
<?php
$a = fopen('php://memory', 'r');
$b = fopen('php://temp', 'r+');

echo get_resource_type($a), PHP_EOL;
echo get_resource_type($b), PHP_EOL;

fclose($a);
echo get_resource_type($a), PHP_EOL;
var_dump(is_resource($a));

fclose($b);

How It Works

  1. Two different stream sources, php://memory and php://temp, are both of the type stream.
  2. After fclose($a), the variable still exists, but PHP no longer treats it as an open resource.
  3. get_resource_type() now returns Unknown, instead of throwing an error.
  4. 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;

How It Works

  1. is_resource() is true only for open resources, so get_resource_type() is called safely.
  2. For an open stream, the function returns Resource of type stream.
  3. After fclose(), is_resource() is false, and get_debug_type() describes the value as resource (closed).
  4. 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.

Next: gettype()

gettype() returns the type of a variable as a string, such as "integer", "string" or "arra.

gettype() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful