Get file system path from PHP stream thread?

If I have a file in php streamwrapper, for example "media://icon.png" , how can I get it to indicate the path to the file system of this file, assuming it is on the file system?

I looked at the documentation page but did not see the streamwrapper path return method.

+2
php
Jun 22 '15 at 16:24
source share
1 answer

The StreamWrapper class represents shared streams. Since not all streams are supported by the concept of a file system, there is no general method for this.

If the uri property of stream_get_meta_data does not work for your implementation , you can record information for open time and then access it through 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 is always a brute force approach: after creating your resource, you can call fstat , which includes the device and the inode. Then you can open this device, go through its directory structure and find this inode. See here for an example.

+1
Jun 22 '15 at 17:08
source share



All Articles