PHPCon Poland 2024

Using Phar Archives: the phar stream wrapper

The Phar stream wrapper fully supports fopen() for read and write (not append), unlink(), stat(), fstat(), fseek(), rename() and directory stream operations opendir() and rmdir() and mkdir().

Individual file compression and per-file metadata can also be manipulated in a Phar archive using stream contexts:

= stream_context_create(array('phar' =>
'compress' => Phar::GZ)),
'metadata' => array('user' => 'cellog')));
file_put_contents('phar://my.phar/somefile.php', 0, $context);

The phar stream wrapper does not operate on remote files, and cannot operate on remote files, and so is allowed even when the allow_url_fopen and allow_url_include INI options are disabled.

Although it is possible to create phar archives from scratch just using stream operations, it is best to use the functionality built into the Phar class. The stream wrapper is best used for read-only operations.

add a note

User Contributed Notes 2 notes

staff at pro-unreal dot de
13 years ago
Please note that the phar stream wrapper does not work with any glob.
When you decide to move your project to phar archives you need to consider this.

The following won't work:

While the following will work:
new DirectoryIterator('phar://some.phar/');
carl at dot dot com
13 years ago
Some Examples of how to use the stream wrapper would be really helpful.
My floundering attempts reveal only the following:

= new PharData(dirname(__FILE__).'/', 0,'phartest',Phar::ZIP) ;

'this is just some test text');

// This works
echo file_get_contents('phar://');

//This Fails
'Thist is text for testfile.txt');

$context = stream_context_create(
'phar' =>array('compress' =>Phar::ZIP))
) ;

//This Fails
'Thist is text for testfile.txt',0,$context);

// This works but only with 'r' readonly mode.
$f = fopen(
'r') ;
To Top