Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 324
Handsome Nerd Avatar asked Mar 18 '23 00:03

Handsome Nerd


2 Answers

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)
like image 127
cweiske Avatar answered Mar 28 '23 21:03

cweiske


(@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
);
like image 26
Patrick Webster Avatar answered Mar 28 '23 23:03

Patrick Webster