phpdeploymentbuildphar

Packaging a PHP application


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

Solution

  • 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)