Create files for your app:
- project_location
- create.php
- src
- index.php
create.php will be generator for phar archives, and inside src will be php application.
<?php
// src/index.php
echo 'hello world';
<?php
// create.php
$pharFile = 'scriptun.phar';
$p = new Phar($pharFile);
$p->buildFromDirectory('src/');
$p->setDefaultStub('index.php', '/index.php');
Now run from terminal:
php project_location/create.php
Phar should be created, but you can have error message:
Uncaught UnexpectedValueException: creating archive "scriptun.phar" disabled by the php.ini setting phar.readonly
Edit your php.ini and set correct value, find "phar.readonly" and set:
# php.ini
phar.readonly = Off
If Phar was successfully created you should see output from your application:
php scriptun.phar
> hello world
Phar application can be compressed:
<?php
// create.php
$p = new Phar('scriptun.phar');
$p->buildFromDirectory('src/');
$p->setDefaultStub('index.php', '/index.php');
// compression examples
$p->compress(Phar::GZ);
$p->compress(Phar::BZ2);
Additionally there will be bz2 and gz file generated. You can use it like phar file:
php scriptun.phar.bz2
Add new comment