Home /Core PHP /How to create phar file in php

How to create phar file in php

PHAR files are treated as read-only by default, and you can use any PHAR file without any special configuration. This is great for deployment. But as you’ll be creating your own PHARs you’ll need to allow write-access which is done through the php.ini file.

Open php.ini, find the phar.readonly directive, and modify it accordingly:

phar.readonly = 0

Now you’re ready to package your libraries and applications as PHARs.

Your First PHAR

Begin by creating the application’s directory structure; somewhere on your system, create the following:


The build directory will hold the PHAR package when it’s created to avoid polluting the source tree with generated artifacts. The src directory holds the source files that make up the application.

index.php will serve as the application’s entry point, common.php will be a pseudo-library of common classes used by the application, and config.ini will be the configuration file for the application.

The contents of index.php looks like:

require_once “phar://myapp.phar/common.php”;
$config = parse_ini_file(“config.ini”);

The contents of common.php looks like:
class AppManager
public static function run($config) {
echo “Application is now running with the following configuration… “;


And the contents of config.ini looks like:


Creating the PHAR

Besides the application structure, you also need a script to generate the PHAR archive. Create a new PHP file named create-phar.php in your myapp root with the following code:

$srcRoot = “~/myapp/src”;

$buildRoot = “~/myapp/build”;

$phar = new Phar($buildRoot . “/myapp.phar”,

FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME, “myapp.phar”);

$phar[“index.php”] = file_get_contents($srcRoot . “/index.php”);

$phar[“common.php”] = file_get_contents($srcRoot . “/common.php”);


copy($srcRoot . “/config.ini”, $buildRoot . “/config.ini”);

Then open a terminal window, navigate to the myapp directory and run it:

aabouzekry@platinum:~/myapp$ php create-phar.php

After running the script, you should find the myapp.phar archive in the build directory along with a copy of config.ini file. Copy these two files to your web server’s document root directory (e.g. htdocs).


To access the Phar packaged application you could call the archive directly, but this is not recommended and may require additional configuration of your web server to forward PHAR files to the correct PHP handler. Another approach is to create a run script which includes the archive.

Create a run script called run.php in your web server’s document root with the following:

require “myapp.phar”;


The code does nothing but bypass the hassle of reconfiguring your server to handle PHAR files directly. If you’re hosting your application in a shared hosting environment and don’t have access to your server’s configuration, then this is a perfect solution as well!

After creating the runner, your web root should look like this:


Point your browser to the run.php script and you should see the following output:

Leave a Reply