Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if PHP files is Running as Part of a `phar` archive

Tags:

php

phar

Is there a way to determine, at runtime, if a PHP file is running as part of a phar archive?

i.e., a native implementation might look something like this

function isRunningAsPhar()
{
    $first_include = get_included_files()[0];
    return strpos($first_include, '.phar') !== false;
}

However, this might not work if the user has renamed the phar to have a different file extension, or symlinked the phar to remove the file extension.

like image 252
Alan Storm Avatar asked Jan 04 '16 18:01

Alan Storm


1 Answers

You could use the function Phar::running(); That gives you a path for the executed phar archive. If the path is set then its an archive.

https://secure.php.net/manual/en/phar.running.php

Example from manual:

<?php
$a = Phar::running(); // $a is "phar:///path/to/my.phar"
$b = Phar::running(false); // $b is "/path/to/my.phar"
?>
like image 95
René Höhle Avatar answered Nov 06 '22 05:11

René Höhle