Instantly share code, notes, and snippets.

@odan /a_phar.md
Last active Sep 18, 2018

Embed
What would you like to do?
Create a PHP Phar file

Create a PHP Phar file

Run: php create-phar.php

app.phar successfully created

Start a phar file

Run: php app.phar

App started

<?php
// Filename: app/index.php
// Optional load composer autoloader
//require_once __DIR__ . '/vendor/autoload.php';
echo "App started";
<?php
// better set to php.ini phar.readonly = 0
ini_set("phar.readonly", 0);
$pharFile = 'app.phar';
// clean up
if (file_exists($pharFile)) {
unlink($pharFile);
}
if (file_exists($pharFile . '.gz')) {
unlink($pharFile . '.gz');
}
// create phar
$p = new Phar($pharFile);
// creating our library using whole directory
$p->buildFromDirectory('app/');
// pointing main file which requires all classes
$p->setDefaultStub('index.php', '/index.php');
// plus - compressing it into gzip
$p->compress(Phar::GZ);
echo "$pharFile successfully created";
@PioneerDark

This comment has been minimized.

Show comment
Hide comment
@PioneerDark

PioneerDark commented Feb 2, 2018

nice

@sohelamin

This comment has been minimized.

Show comment
Hide comment
@sohelamin

sohelamin May 21, 2018

Awesome (y)

sohelamin commented May 21, 2018

Awesome (y)

@izharaazmi

This comment has been minimized.

Show comment
Hide comment
@izharaazmi

izharaazmi Jun 19, 2018

You cannot set ini_set("phar.readonly", 0); directive in runtime. It must only be set in php.ini file for security reasons. See Manual at php.net

izharaazmi commented Jun 19, 2018

You cannot set ini_set("phar.readonly", 0); directive in runtime. It must only be set in php.ini file for security reasons. See Manual at php.net

@warapongkusungnoen

This comment has been minimized.

Show comment
Hide comment
@warapongkusungnoen

warapongkusungnoen Aug 29, 2018

I have tried to pack language pack for osticket back to .phar file
what is index.php? and where I should place it
after I run app.phar.it show message

PHP Warning: include(phar://C:\xampp\htdocs\convert\phar\app.phar/index.php): failed to open stream: phar error: "index.php" is not a file in phar "C:/xampp/htdocs/convert/phar/app.phar" in C:\xampp\htdocs\convert\phar\app.phar on line 9

Warning: include(phar://C:\xampp\htdocs\convert\phar\app.phar/index.php): failed to open stream: phar error: "index.php" is not a file in phar "C:/xampp/htdocs/convert/phar/app.phar" in C:\xampp\htdocs\convert\phar\app.phar on line 9
PHP Warning: include(): Failed opening 'phar://C:\xampp\htdocs\convert\phar\app.phar/index.php' for inclusion (include_path='phar://C:\xampp\htdocs\convert\phar\app.phar;C:\xampp\php\PEAR') in C:\xampp\htdocs\convert\phar\app.phar on line 9

Warning: include(): Failed opening 'phar://C:\xampp\htdocs\convert\phar\app.phar/index.php' for inclusion (include_path='phar://C:\xampp\htdocs\convert\phar\app.phar;C:\xampp\php\PEAR') in C:\xampp\htdocs\convert\phar\app.phar on line 9

warapongkusungnoen commented Aug 29, 2018

I have tried to pack language pack for osticket back to .phar file
what is index.php? and where I should place it
after I run app.phar.it show message

PHP Warning: include(phar://C:\xampp\htdocs\convert\phar\app.phar/index.php): failed to open stream: phar error: "index.php" is not a file in phar "C:/xampp/htdocs/convert/phar/app.phar" in C:\xampp\htdocs\convert\phar\app.phar on line 9

Warning: include(phar://C:\xampp\htdocs\convert\phar\app.phar/index.php): failed to open stream: phar error: "index.php" is not a file in phar "C:/xampp/htdocs/convert/phar/app.phar" in C:\xampp\htdocs\convert\phar\app.phar on line 9
PHP Warning: include(): Failed opening 'phar://C:\xampp\htdocs\convert\phar\app.phar/index.php' for inclusion (include_path='phar://C:\xampp\htdocs\convert\phar\app.phar;C:\xampp\php\PEAR') in C:\xampp\htdocs\convert\phar\app.phar on line 9

Warning: include(): Failed opening 'phar://C:\xampp\htdocs\convert\phar\app.phar/index.php' for inclusion (include_path='phar://C:\xampp\htdocs\convert\phar\app.phar;C:\xampp\php\PEAR') in C:\xampp\htdocs\convert\phar\app.phar on line 9

@loevgaard

This comment has been minimized.

Show comment
Hide comment
@loevgaard

loevgaard Sep 18, 2018

For other people coming here and wanting to 'up their phar game', take a look at this article: Create and deploy secure PHARs

loevgaard commented Sep 18, 2018

For other people coming here and wanting to 'up their phar game', take a look at this article: Create and deploy secure PHARs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment