PHP stream_wrapper_restore() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_wrapper_restore() restores a previously unregistered built-in protocol wrapper.

After stream_wrapper_unregister('http') or similar, call stream_wrapper_restore('http') to bring the original handler back. It returns true on success. Use this when you temporarily replace a built-in scheme for a test, then want the default behavior again.

Syntax

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

PHP
$ok = stream_wrapper_restore('http');
CallWhat it does
stream_wrapper_restore('http')Restores the built-in http wrapper after unregister.
stream_wrapper_restore('file')Restores the built-in file wrapper if it was unregistered.
stream_wrapper_restore($protocol)Restores whatever built-in scheme name is in $protocol.

Restore is meant for built-in wrappers you removed with stream_wrapper_unregister(). It does not recreate a custom class you registered yourself. For custom schemes, register again with stream_wrapper_register().

Library declarations
PHP
stream_wrapper_restore(string $protocol): bool

Parameters

ParameterDescription
protocolRequired. The built-in scheme name to restore, such as http or file.

Return Value

Return ValueDescription
trueThe built-in wrapper was restored.
falseRestore failed.

PHP Examples

Example 1 — Restore After Unregister Sketch

Show the unregister-then-restore sequence with fixed tutorial output.

PHP
<?php
// Example flow for a built-in scheme:
// stream_wrapper_unregister('http');
// ... temporary custom http handler ...
// stream_wrapper_restore('http');
echo 'restore target: http', PHP_EOL;
echo 'on success: true', PHP_EOL;

How It Works

  1. Unregister the built-in scheme first.
  2. Optionally register a temporary replacement.
  3. stream_wrapper_restore() puts the original back.
  4. true means the built-in handler returned.

Example 2 — Custom Scheme Uses Register Again

Demonstrate that a custom protocol is brought back with register, not restore.

PHP
<?php
class TempStream {
    public $context;
    public function stream_open($path, $mode, $options, &$opened_path) { return true; }
    public function stream_read($count) { return 'x'; }
    public function stream_eof() { return true; }
    public function stream_stat() { return []; }
}

stream_wrapper_register('tempdemo', TempStream::class);
stream_wrapper_unregister('tempdemo');
$ok = stream_wrapper_register('tempdemo', TempStream::class);
echo $ok ? 'custom scheme back' : 'failed', PHP_EOL;

How It Works

  1. Register a custom tempdemo scheme.
  2. Unregister removes it.
  3. Register again to restore a custom class.
  4. stream_wrapper_restore() is for built-ins, not this case.

Example 3 — Confirm Wrappers List Idea

Print a fixed reminder to check stream_get_wrappers() after restore.

PHP
<?php
echo 'after restore, check stream_get_wrappers()', PHP_EOL;
echo 'built-in schemes reappear in that list', PHP_EOL;

How It Works

  1. stream_get_wrappers() lists active schemes.
  2. A restored built-in should appear again.
  3. Use the list while debugging register/unregister mistakes.
  4. Keep production code from unregistering core schemes casually.

Frequently Asked Questions

It restores a built-in protocol wrapper that was removed with stream_wrapper_unregister().
No. For custom schemes, call stream_wrapper_register() again with your class.
When a test temporarily replaces a built-in like http and must put the original handler back.
Restore failed, for example if the protocol was not a restorable built-in wrapper.
Check the boolean return and confirm the scheme appears in stream_get_wrappers().

Next: stream_wrapper_unregister()

stream_wrapper_unregister() removes a protocol scheme from the list of active stream wrapp.

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