PHP Luminova: Access Local Files Through a Stream Wrapper
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
- Full namespace:
Luminova\Storage\StreamWrapper - This class extends Luminova\Base\AbstractStreamWrapper.
- This class is a concrete class.
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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$scheme | string | Scheme 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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$path | string | Wrapper URL containing a scheme and local path. |
$mode | string | PHP file mode. |
$options | int | PHP stream opening flags (default: 0). |
$openedPath | string\|null | Output 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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$count | int | Maximum number of bytes to read. |
Return:
string - Bytes read, or an empty string if the underlying read returns false.
Throws:
- Luminova\Exceptions\RuntimeException if the underlying file resource is unavailable or closed.
stream_write
Write bytes to the underlying file.
public function stream_write(string $data): intParameters:
| Parameter | Type | Description |
|---|---|---|
$data | string | Bytes to write. |
Return:
int - Number of bytes written, or zero when the underlying write returns false.
Throws:
- Luminova\Exceptions\RuntimeException if the underlying file resource is unavailable or closed.
stream_seek
Move the underlying file cursor.
public function stream_seek(int $offset, int $whence = SEEK_SET): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$offset | int | Byte offset. |
$whence | int | Offset origin, such as SEEK_SET, SEEK_CUR, or SEEK_END (default: SEEK_SET). |
Return:
bool - true on success, or false on failure.
Throws:
- Luminova\Exceptions\RuntimeException if the underlying file resource is unavailable or closed.
stream_tell
Read the current file position.
public function stream_tell(): intReturn:
int - Byte offset, or zero on failure.
Throws:
- Luminova\Exceptions\RuntimeException if the underlying file resource is unavailable or closed.
stream_close
Close the underlying file resource.
public function stream_close(): voidThrows:
- Luminova\Exceptions\RuntimeException if the underlying file resource is unavailable or closed.
stream_eof
Check whether the underlying stream has reached its end.
public function stream_eof(): boolReturn:
bool - Whether the stream is at its end.
Throws:
- Luminova\Exceptions\RuntimeException if the underlying file resource is unavailable or closed.
stream_stat
Read metadata for the open file.
public function stream_stat(): arrayReturn:
array<int|string,int> - PHP fstat() fields, or an empty array on failure.
Throws:
- Luminova\Exceptions\RuntimeException if the underlying file resource is unavailable or closed.
stream_flush
Flush buffered output to the underlying file.
public function stream_flush(): boolReturn:
bool - true on success, or false on failure.
Throws:
- Luminova\Exceptions\RuntimeException if the underlying file resource is unavailable or closed.
unlink
Remove an existing local file addressed by the wrapper.
public function unlink(string $path): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$path | string | Wrapper 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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$from | string | Source wrapper URL. |
$to | string | Destination 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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$path | string | Wrapper URL containing a scheme and local path. |
$mode | int | Mode bits subject to the process umask (default: 0775). |
$options | int | Include 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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$from | string | Source wrapper URL. |
$to | string | Destination 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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$path | string | Wrapper URL containing a scheme and local path. |
$options | int | Removal 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): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$path | string | Wrapper URL containing a scheme and local path. |
$flags | int | Metadata 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|falseReturn:
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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$directory | string | Wrapper URL containing a scheme and local path. |
$options | int | Opening 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(): boolReturn:
bool - Always true.
dir_closedir
Clear the shared directory listing and reset its cursor.
public function dir_closedir(): boolReturn:
bool - Always true.