"get filesystem path from a php streamwrapper?" Code Answer

5

the streamwrapper class represents generic streams. because not all streams are backed by the notion of a filesystem, there isn't a generic method for this.

if the uri property from stream_get_meta_data isn't working for your implementation, you can record the information during open time then access it via stream_get_meta_data. example:

class mediastream {
    public $localpath = null;

    public function stream_open($path, $mode, $options, &$opened_path)
    {
        $this->localpath = realpath(preg_replace('+^media://+', '/', $path));

        return fopen($this->localpath, $mode, $options);
    }
}

stream_wrapper_register('media', 'mediastream');

$fp   = fopen('media://tmp/icon.png', 'r');
$data = stream_get_meta_data($fp);
var_dump(
    $data['wrapper_data']->localpath
);

of course, there's always a brute force approach: after creating your resource, you can call fstat, which includes the device and inode. you can then open that device, walk its directory structure, and find that inode. see here for an example.

By strv7 on April 24 2022

Answers related to “get filesystem path from a php streamwrapper?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.