要从 Unix shell 脚本设置 ini 键 phar.readonly 为 0,可以在运行时进行,如下所示。
此页面上给出的示例在引号方面存在一些问题:(请参阅转换为单引号的转换)
#!/usr/bin/php -d phar.readonly=0
<?php
print(ini_get('phar.readonly')); @unlink('brandnewphar.phar');
try {
$p = new Phar(dirname(__FILE__) . '/brandnewphar.phar', 0, 'brandnewphar.phar');
} catch (Exception $e) {
echo '无法创建 phar:', $e;
}
echo '新的 phar 有 ' . $p->count() . " 个条目\n";
$p->startBuffering();
$p['file.txt'] = 'hi';
$p['file2.txt'] = 'there';
$p['file2.txt']->compress(Phar::GZ);
$p['file3.txt'] = 'babyface';
$p['file3.txt']->setMetadata(42);
$p->setStub('<?php
function __autoload($class)
{
include "phar://myphar.phar/" . str_replace("_", "/", $class) . ".php";
}
Phar::mapPhar("myphar.phar");
include "phar://myphar.phar/startup.php";
__HALT_COMPILER();');
$p->stopBuffering();
$m = file_get_contents("phar://brandnewphar.phar/file2.txt");
$m = explode("\n",$m);
var_dump($m);