Speed up development with full-stack environments for every branch.

Learn More

How to create a compressed Phar archive [PHP]

127 Runs 1467 Views 655 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/*
 * PHP Phar - How to create a compressed Phar archive
 */

ini_set('display_errors', 1);


$p = new Phar('my.phar', FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME, 'my.phar');
//issue the Phar::startBuffering() method call to buffer changes made to the archive until you issue the Phar::stopBuffering() command
$p->startBuffering();

//set the signature algorithm for the phar archive
$p->setSignatureAlgorithm(Phar::SHA256);

//set the Phar file stub
//the file stub is merely a small segment of code that gets run initially when the Phar file is loaded, 
//and it always ends with a __HALT_COMPILER() 
$p->setStub('<?php Phar::mapPhar(); include "phar://my.phar/index.php"; __HALT_COMPILER(); ?>');

//Adding files to the archive
$p['text.txt'] = 'This is a text file';
//Adding files to an archive using Phar::buildFromDirectory()
//adds all of the PHP files in the stated directory to the Phar archive
$p->buildFromDirectory('include/', '$(.*)\.php$');

//Compresses the entire Phar archive using Gzip or Bzip2 compression
$p->compress(Phar::GZ);
//Note that a regular .phar archive will also be created besides the compresssed one

//Stop buffering write requests to the Phar archive, and save changes to disk
$p->stopBuffering();
echo "phar archive has been saved";

?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).