PHP 8.3.4 Released!

Phar アーカイブの使用法: phar ストリームラッパー

Phar ストリームラッパーは、fopen() による読み込みや 書き込み (追記は除く)、unlink()stat()fstat()fseek()rename() そしてディレクトリに対する opendir()rmdir()mkdir() といった機能に完全に対応しています。

Phar アーカイブ内の各ファイルの圧縮やファイル単位のメタデータの操作も、 ストリーム上で可能です。

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

phar ストリームラッパーは、 リモートファイルを操作することはできません。つまり、INI 設定 allow_url_fopen および allow_url_include が無効になっている場合でも使用できます。

ストリーム操作だけで新しい phar アーカイブをゼロから作成することも可能ですが、 そんな場合は Phar クラスの組み込み機能を使用するほうが便利です。 ストリームラッパーが有用なのは、読み込み専用の操作を行う場合です。

add a note

User Contributed Notes 2 notes

up
2
staff at pro-unreal dot de
12 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:
<?php
glob
('phar://some.phar/*');
new
DirectoryIterator('glob://phar://some.phar/*');
?>

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

<?php
$p
= new PharData(dirname(__FILE__).'/phartest.zip', 0,'phartest',Phar::ZIP) ;

$p->addFromString('testfile.txt',
'this is just some test text');

// This works
echo file_get_contents('phar://phartest.zip/testfile.txt');

//This Fails
file_put_contents('phar://phartest.zip/testfile.txt',
'Thist is text for testfile.txt');

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

//This Fails
file_put_contents(
'phar://phartest.zip/testfile.txt',
'Thist is text for testfile.txt',0,$context);

// This works but only with 'r' readonly mode.
$f = fopen(
'phar://C:\\Inetpub\\wwwroot\\PACT\\test\\phartest.zip\\testfile.txt',
'r') ;
?>
To Top