Packaging a PHP application

Related searches

I am trying to create a .phar file from my web application. Following the php documentation's example I tried the following for this purpose.

<?php
$srcRoot = __DIR__ . "/../app";
$buildRoot = __DIR__ . "/../build";
$p = new Phar("$buildRoot/build.phar", 0, 'build.phar');
$p->buildFromIterator(
    new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($srcRoot)
    ),
    $srcRoot
);

However I got the following error. I dont have any idea about the error. What is wrong with the code?

PHP Fatal error:  Uncaught exception 'UnexpectedValueException' with message
'Iterator RecursiveIteratorIterator returned a path "D:\site\app" that is
 not in the base directory "D:\site\app"'
in D:\site\tools\create-phar.php:7

The source of the problem is that RecursiveDirectoryIterator also lists the dot files - . and ... When iterating over /path/to/foo it also lists /path/to/foo/. and /path/to/foo/.. which goes to the parent directory - outside the base directory.

Thus you have to prevent the inclusion of the ".." files, which is most easily achieved with FilesystemIterator::SKIP_DOTS as second parameter to DirectoryIterator:

new RecursiveDirectoryIterator($srcRoot, FilesystemIterator::SKIP_DOTS)

How to package PHP apps so they will work on any user computer , In addition to the XAMPP you can use Phar files that give you the ability to package your PHP applications as a unit for installation and/or� Packaging a PHP application. Ask Question Asked 5 years, 1 month ago. Active 1 month ago. Viewed 922 times 4. I am trying to create a .phar file from my web


(@cweiske -- I just realized you beat me to it, I'll make sure to refresh the page next time, my sincerest apologies!)

You need just a slight edit to skip over the unix paths /. and /..:

<?php
$srcRoot = __DIR__ . "/../app";
$buildRoot = __DIR__ . "/../build";
$p = new Phar("$buildRoot/build.phar", 0, 'build.phar');
$p->buildFromIterator(
    new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($srcRoot, FilesystemIterator::SKIP_DOTS)
    ),
    $srcRoot
);

Setting Up and Deploying a Modern PHP Application, If you're stuck with a legacy PHP application that can't be set up to those Composer for PHP package management; NGINX (or Apache) with� Creating a package. PDO drivers are released via PECL; all the usual rules for PECL extensions apply. Packaging is accomplished by creating a valid package.xml file and then running:


Box makes it very easy to create PHAR archives from your source files. Basically you first add a configuration file and then you can create your PHAR file by simply invoking box build on the command line.

There is a great blog post by Matthieu Moquet describing how he used Box to simplify the distribution of a PHP Cli application. Personally, I've also been using Box for a CLI application, however the official project description is not limited to CLI applications, but rather summarizes the Box project as:

An application for building and managing Phars.

The Box project provides the PHAR build script and takes care of setting all paths correctly, so this might solve your problem.

Packaging Your Apps with Phar, Creating the PHAR. Besides the application structure, you also need a script to generate the PHAR archive. Create a new PHP file named create-� In addition to the XAMPP you can use Phar files that give you the ability to package your PHP applications as a unit for installation and/or deployment. You can configure a separate php file which will handle any database creations and initial configuration which can be set to run on the first time.


The solution proposed by @cweiske is brilliant. In some situations, however, you may need the ability to add more directory path exclusions. Have a look at this example that excludes any references to the .git directory, using FilterIterator:

// create phar
$p = new Phar($pharFile, 0, $pharFile);

// produce iteration which excludes any references to values assigned to $excludes
$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)
);
$filter = new class($iterator) extends FilterIterator {
    public static $excludes = [];
    public function accept()
    {
        $item = $this->current();
        $actual   = 0;
        foreach (self::$excludes as $exclude) {
            $actual += (int) boolval(strpos($item->getPath(), $exclude));
        }
        return ($actual === 0);
    }
};
$filter::$excludes = ['.git'];

$p->buildFromIterator($filter, $path);

PHP: The Right Way, not familiar with it, PHAR (Php Archive) is analogous to the JAR file concept but for PHP. It allows you to package an application into a single� I propose implementing a system for easily packaging PHP applications. A .PAR file (PHP ARchive) file is a zipfile containing the following: * All the images, includes, and php files that make up the PHP application. * a PHP.INI file configured specific to the application. PHP would ignore values from the master PHP.INI file and use the


Have a look at php.ini file to check the value of phar.readonly field. It must be 0 to create phar archive.

Ref: http://php.net/manual/en/phar.configuration.php#ini.phar.readonly

Create and deploy secure PHARs. For those who are not familiar , Check out this excellent article on deploying PHP applications with Deployer. Conclusion. There is no way we can cover all the different options available for deploying PHP applications. PHP is an enterprise language that has evolved over the years, thus calling for more efficient ways for deploying PHP apps from a local machine to production.


See Supported Languages and Platforms for instructions for other platforms. Supported PHP Versions Language Supported Versions PHP 5.2–7.3 Supported PHP Frameworks Framework Versions Laravel 5.x Zend 1, 2, 3 Template Engines Name Supported Versions Smarty 2.6, 3.1 Packaging Guidance Upload a compressed ZIP archive cont


The application problem is an ongoing headache for every organization, but it can be more manageable by standardizing your packaging and using a tool like App-V to move your application deployments into the modern day!


All applications in the Windows Web App Gallery must follow these principles. Read and use this article to create and test a Web Deploy application package. Refer to the article "Best Practices for PHP on Windows" for guidance in running your PHP application. Submit the package to the Gallery for testing using the submission form.