phpphar

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


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.


Solution

  • 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"
    ?>