PHP stream_is_local() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_is_local() reports whether a stream (or a stream URL string) refers to a local resource rather than a remote network wrapper.

Pass either an open stream resource or a path/URL string. Local file paths and many php:// streams return true. Remote wrappers such as http:// normally return false.

Syntax

Call stream_is_local() with the values you need. Start with this line.

PHP
$local = stream_is_local($stream);
CallWhat it does
stream_is_local("C:/data/file.txt")Returns true for an ordinary local file path.
stream_is_local("http://example.com")Returns false because http is a remote wrapper.
stream_is_local($fp)Checks an already-open stream resource.

"Local" means the wrapper is treated as local by PHP, not that a file is safe or writable. Always validate paths separately when you accept user input.

Library declarations
PHP
stream_is_local(resource|string $stream): bool

Parameters

ParameterDescription
streamRequired. An open stream resource, or a string path/URL to test.

Return Value

Return ValueDescription
booltrue when PHP considers the stream or URL local; false otherwise.

PHP Examples

Example 1 — Compare a File Path and an HTTP URL

Print whether a local path and a remote URL are considered local.

PHP
<?php
echo "file path: ", stream_is_local(__FILE__) ? "yes" : "no", PHP_EOL;
echo "http url: ", stream_is_local("http://example.com/a.txt") ? "yes" : "no", PHP_EOL;

How It Works

  1. __FILE__ is the path of the current script on disk.
  2. stream_is_local() returns true for that local path.
  3. An http:// URL uses a remote wrapper, so the result is false.
  4. The demo prints yes and no without opening any network connection.

Example 2 — Test an Open Memory Stream

Open php://memory and ask whether that handle is local.

PHP
<?php
$fp = fopen("php://memory", "r+");
echo stream_is_local($fp) ? "local" : "remote", PHP_EOL;
fclose($fp);

How It Works

  1. php://memory is opened as a stream resource.
  2. stream_is_local() accepts that resource directly.
  3. PHP treats this wrapper as local, so the script prints local.
  4. fclose() closes the handle.

Example 3 — Branch Before Choosing How to Read

Use the boolean result to print different messages for local and remote targets.

PHP
<?php
$targets = [
    "php://temp",
    "https://example.com/readme",
];

foreach ($targets as $target) {
    $label = stream_is_local($target) ? "local" : "remote";
    echo $target, " => ", $label, PHP_EOL;
}

How It Works

  1. Two URL-style strings are tested without opening them.
  2. php://temp is local according to PHP.
  3. https://. .. is remote.
  4. You can use the same check before deciding which options or timeouts to apply.

Frequently Asked Questions

It returns true when a stream resource or URL is considered local, and false when it uses a remote wrapper.
Yes. You can pass a path or URL string, or an open stream resource.
Yes. PHP treats php://memory and php://temp as local streams.
No. false usually means "not local" (for example http or ftp). The URL may still be valid for remote access.
No. It only classifies local versus remote. You still need path validation and careful handling of user-supplied names.

Next: stream_isatty()

stream_isatty() returns true when the given stream is connected to a terminal (TTY), and f.

stream_isatty() 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