简体   繁体   中英

PHP PHAR: Once again about creation of .phar in a proper way

I'm trying to create a PHAR Archive with executable stub. The code:

<?php
$phar = new Phar('test.phar');
$phar->buildFromDirectory('files/');
$phar->setStub($phar->createDefaultStub(file_get_contents('st.php'), 'install.php'));
?>

The contents of "st.php" (the default stub):

#!/usr/bin/php
<?php Phar::mapPhar(); include("phar://test.phar/install.php"); __HALT_COMPILER();

Inside the "files" is only one file - the "install.php"

<?php
echo "The Installer is running right now.";
?>

PHAR compilation process runs with no any errors. But when I try to include the created PHAR archive

include_once('phar://test.phar');

the error occurs:

PHP Parse error:  syntax error, unexpected ''#!/usr/bin/php\r' (T_ENCAPSED_AND_WHITESPACE) in D:/WebServer/domains/modinst/www/test.phar on line 110

The question is: how to create a PHAR archive for use as a PHP resource that I can simply 'include'?

You cannot use the shebang line within the stub file. This is a limitation of current PHP versions.

Remove it and the error will disappear.

To set the shebang, use:

$phar->setStub("#!/usr/bin/env php" . PHP_EOL 
    . $phar->createDefaultStub(file_get_contents('st.php')));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM