I am trying to create a "virtual" file without using memory or a temporary file. A "virtual" file should pass the test done using file_exists() without causing any errors or warnings when used with require or include .
Allows you to implement your own handlers and protocol threads for use with all other file system functions (for example, fopen() , fread() , etc.).
... where file_exists() is one of them. The documentation page states:
Starting with PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which shells support the stat() family of functions.
My attempt to create a custom virtual file wrapper
class VirtualFileWrapper { public $context; public function stream_open( $path, $mode, $options, &$opened_path ) { return TRUE; } public function stream_stat() { var_dump( __METHOD__ ); $data = [ 'dev' => 0, 'ino' => getmyinode(), 'mode' => 'r', 'nlink' => 0, 'uid' => getmyuid(), 'gid' => getmygid(), 'rdev' => 0, 'size' => 0, 'atime' => time(), 'mtime' => getlastmod(), 'ctime' => FALSE, 'blksize' => 0, 'blocks' => 0, ]; return array_merge( array_values( $data ), $data ); } } stream_wrapper_register( 'virtual', 'VirtualFileWrapper' ); $file = fopen( "virtual://foo", 'r+' );
While the fstat() function executes this method, file_exists() does not execute any stream class method.
How can I make a virtual thread wrapper work (with file_exists() )?
I fully understand that tempnam( __DIR__, '' ) will go through both:
var_dump( tempnam( __DIR__, '' ) ); Returns truerequire tempnam( __DIR__, '' ); Error
but I do not want to use a temporary file, as there may be a better way (in terms of performance).