Luminova Framework

PHP Luminova: Access Local Files Through a Stream Wrapper

Last updated: 2026-11-28 20:57:12

Register a custom stream scheme for local files and use it with PHP filesystem functions.

Use StreamWrapper to add a scheme to trusted local paths. The wrapper removes the scheme and passes the remaining path to PHP filesystem functions. It does not restrict paths to a storage root. Choose an unused application-specific scheme. Use register() for ordinary setup. It leaves existing registrations in place.


Usage

use Luminova\Storage\StreamWrapper;

$scheme = 'lmv-local-example';
$path = tempnam(sys_get_temp_dir(), 'lmv-');

if ($path !== false) {
    file_put_contents($path, 'Sample report');

    if (StreamWrapper::register($scheme)) {
        try {
            echo file_get_contents($scheme . '://' . $path); // Sample report
        } finally {
            StreamWrapper::unregister($scheme);
        }
    }

    unlink($path);
}

Class Definition


Inherited API

The base class provides $context, the constructor, and shared protocol operations such as unregister(), restore(), isRegistered(), and protocol(). See AbstractStreamWrapper for the shared API and callback defaults.


Methods

register

Register the wrapper for a scheme through the base class. On success, record the scheme for local path handling and reset directory iteration state. Use this method to initialize local path handling. The inherited reassign() method alone does not update the local scheme list.

public static function register(string $scheme): bool

Parameters:

ParameterTypeDescription
$schemestringScheme name without ://.

Return:

bool - true on success, or false on failure.


stream_open

Open the local path with the requested mode. If the parent directory is missing, try to create it before opening the file. The current implementation does not use the opening flags.

public function stream_open(string $path, string $mode, int $options = 0, ?string &$openedPath = null): bool

Parameters:

ParameterTypeDescription
$pathstringWrapper URL containing a scheme and local path.
$modestringPHP file mode.
$optionsintPHP stream opening flags (default: 0).
$openedPathstring\|nullOutput argument set to the local path on success (default: null).

Return:

bool - true on success, or false on failure.


stream_read

Read from the current file position.

public function stream_read(int $count): string

Parameters:

ParameterTypeDescription
$countintMaximum number of bytes to read.

Return:

string - Bytes read, or an empty string if the underlying read returns false.

Throws:


stream_write

Write bytes to the underlying file.

public function stream_write(string $data): int

Parameters:

ParameterTypeDescription
$datastringBytes to write.

Return:

int - Number of bytes written, or zero when the underlying write returns false.

Throws:


stream_seek

Move the underlying file cursor.

public function stream_seek(int $offset, int $whence = SEEK_SET): bool

Parameters:

ParameterTypeDescription
$offsetintByte offset.
$whenceintOffset origin, such as SEEK_SET, SEEK_CUR, or SEEK_END (default: SEEK_SET).

Return:

bool - true on success, or false on failure.

Throws:


stream_tell

Read the current file position.

public function stream_tell(): int

Return:

int - Byte offset, or zero on failure.

Throws:


stream_close

Close the underlying file resource.

public function stream_close(): void

Throws:


stream_eof

Check whether the underlying stream has reached its end.

public function stream_eof(): bool

Return:

bool - Whether the stream is at its end.

Throws:


stream_stat

Read metadata for the open file.

public function stream_stat(): array

Return:

array<int|string,int> - PHP fstat() fields, or an empty array on failure.

Throws:


stream_flush

Flush buffered output to the underlying file.

public function stream_flush(): bool

Return:

bool - true on success, or false on failure.

Throws:


Remove an existing local file addressed by the wrapper.

public function unlink(string $path): bool

Parameters:

ParameterTypeDescription
$pathstringWrapper URL containing a scheme and local path.

Return:

bool - true on success, or false on failure.


rename

Rename or move a local path. The source must use a registered wrapper scheme. The destination must also contain a scheme, which is stripped before the local rename.

public function rename(string $from, string $to): bool

Parameters:

ParameterTypeDescription
$fromstringSource wrapper URL.
$tostringDestination wrapper URL.

Return:

bool - true on success, or false on failure.


mkdir

Create a local directory addressed by the wrapper.

public function mkdir(string $path, int $mode = 0775, int $options = 0): bool

Parameters:

ParameterTypeDescription
$pathstringWrapper URL containing a scheme and local path.
$modeintMode bits subject to the process umask (default: 0775).
$optionsintInclude STREAM_MKDIR_RECURSIVE to create missing parents (default: 0).

Return:

bool - true on success, or false on failure.


copy

Copy a local file through this wrapper method. PHP’s global copy() normally uses stream callbacks. This method translates both URLs to local paths before calling the native filesystem copy.

public function copy(string $from, string $to): bool

Parameters:

ParameterTypeDescription
$fromstringSource wrapper URL.
$tostringDestination wrapper URL.

Return:

bool - true on success, or false on failure.


rmdir

Remove an empty directory addressed by the wrapper.

public function rmdir(string $path, int $options = 0): bool

Parameters:

ParameterTypeDescription
$pathstringWrapper URL containing a scheme and local path.
$optionsintRemoval flags. This callback does not use them (default: 0).

Return:

bool - true on success, or false on failure.


url_stat

Read local metadata for a wrapper URL.

public function url_stat(string $path, int $flags): array

Parameters:

ParameterTypeDescription
$pathstringWrapper URL containing a scheme and local path.
$flagsintMetadata flags supplied by PHP. This implementation uses stat() regardless of the flags.

Return:

array<int|string,int> - PHP stat() fields, or an empty array if the path cannot be resolved or queried.


dir_readdir

Read and advance to the next directory entry.

public function dir_readdir(): string|false

Return:

string|false - Entry name, or false when no entry remains.


dir_opendir

Load directory entries and reset the shared cursor. Directory state is static, so opening another directory can replace the current listing.

public function dir_opendir(string $directory, int $options = 0): bool

Parameters:

ParameterTypeDescription
$directorystringWrapper URL containing a scheme and local path.
$optionsintOpening flags. This callback does not use them (default: 0).

Return:

bool - true on success, or false on failure.


dir_rewinddir

Reset the shared directory cursor to the first entry.

public function dir_rewinddir(): bool

Return:

bool - Always true.


dir_closedir

Clear the shared directory listing and reset its cursor.

public function dir_closedir(): bool

Return:

bool - Always true.