Skip to content

Instantly share code, notes, and snippets.

@GaryRogers
Last active March 28, 2020 23:54
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GaryRogers/d67c95dbf3e77c330bbf to your computer and use it in GitHub Desktop.
Save GaryRogers/d67c95dbf3e77c330bbf to your computer and use it in GitHub Desktop.
Create a Phar archive for PHP
<?php
# Create an executable phar
# Name of our archive.
$phar = new Phar("myscript.phar");
# Have to do buffering to make things executable.
# See http://stackoverflow.com/questions/11082337/how-to-make-an-executable-phar
$phar->startBuffering();
# Default executable.
$defaultStub = $phar->createDefaultStub('bin/myscript.php');
# Build from the project directory. Assumes that createPhar.php (this file) is in the project root.
$phar->buildFromDirectory(dirname(__FILE__));
# Add the header to enable execution.
$stub = "#!/usr/bin/env php \n" . $defaultStub;
# Set the stub.
$phar->setStub($stub);
# Wrap up.
$phar->stopBuffering();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment