Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Build a PHAR without hidden files
//Cleaning up any existing phar
//Where are my application sources
$dir = '../app/';
//Quickly get a recursive folder list
$folder = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
$items = array();
foreach($folder as $item)
//Get the filename
$filename = pathinfo($item->getPathName(), PATHINFO_BASENAME);
//Filter Unix hidden files by their leading dot. Add more filters in this condition.
if(substr($filename, 0, 1) != '.')
//Key is the relative path inside the Phar, value is the complete path of the file
$items[substr($item->getPathName(), strlen($dir))] = $item->getPathName();
//Shows the packaged files
$phar = new Phar('package.phar');
//Populate the Phar
$phar->buildFromIterator(new ArrayIterator($items));
//Add a stub
echo PHP_EOL;
//Listing the Phar's content
$phar = new Phar(__FILE__);
foreach (new RecursiveIteratorIterator($phar) as $iteration)
echo $iteration->getPathName().PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.