Skip to main content

Creating Phar archives

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

 

Continue reading with:

Add new comment

CAPTCHA

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

We use cookies on our website to enhance your user experience. We also use Google analytics and ads.

Click here to read more I've read it