Phar档案输出乱码

Pas*_*cal 2 php apache phar

我用以下代码创建了我的第一个phar档案:

$phar = new Phar('myphar.phar');
$phar->addFile("index.php");
$phar->setStub($phar->createDefaultStub('index.php', 'index.php'));
Run Code Online (Sandbox Code Playgroud)

提到的index.php只做一个输出:

echo "I am in a PHP archive!";
Run Code Online (Sandbox Code Playgroud)

当我运行上面的代码时,会创建myphar.phar,当我在cli中运行时,输出是"我在PHP存档中!".但是,当我从webbrowser调用myphar.phar时,它会输出一些奇怪的字符,比如??????????我的index.php内容而不是错误.

我将以下行添加到我的apache httpd.conf中以支持phar档案:

AddType application/x-httpd-php .phar
Run Code Online (Sandbox Code Playgroud)

有人知道它为什么在cli上工作,而不是在浏览器中?

pil*_*lif 6

这个问题的答案在于

detect_unicode
Run Code Online (Sandbox Code Playgroud)

在php.ini中设置.

目前PHP中存在一个错误(http://bugs.php.net/bug.php?id=42396),如果启用了detect_unicode,则会使__halt_compiler()无法正常工作.

因此,作为临时解决方法,请设置

detect_unicode = Off
Run Code Online (Sandbox Code Playgroud)

在你的php.ini中.您必须付出的代价是无法使用unicode字符作为变量,函数和类的名称(它们在注释和字符串中仍然很好)