Phar archive outputs jibberish
I created my first phar archive with the following code:
$phar = new Phar('myphar.phar');
$phar->addFile("index.php");
$phar->setStub($phar->createDefaultStub('index.php', 'index.php'));
The specified index.php makes only one output:
echo "I am in a PHP archive!";
When I run the above code, myphar.phar is generated, and when I run in the cli, the output is "I'm in a PHP archive!". However, when I call myphar.phar from a web browser, it prints some strange characters, like ???? ??? ?
my index.php content and errors instead of my content.
I added the following line to apache httpd.conf to support phar archives:
AddType application/x-httpd-php .phar
Does anyone know why it works in the cli but not in the browser?
source to share
The answer to this problem lies in
detect_unicode
in your php.ini.
There is currently a bug in PHP ( http://bugs.php.net/bug.php?id=42396 ) that causes __halt_compiler () to malfunction if the detect_unicode option is enabled.
So, as a temporary workaround, install
detect_unicode = Off
in your php.ini. The price you have to pay is the inability to use Unicode characters as the name of variables, functions and classes (they still remain within comments and lines)
source to share
My understanding of a phar file, you can open (fopen) a file in your php script that you are about to call .... if you return these characters it means apache is sending you the file in binary ...
if you add this application "AddType application / x-gzip.gz.tgz.phar" to your apache config, it will make the file downloadable like any other archive file and will not execute it as a PHP script
source to share