tags:

views:

204

answers:

4

I've 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 mentioned index.php only does a single output:

echo "I am in a PHP archive!";

When I run the above code, myphar.phar is created and when I run in at the cli, the output is "I am in a PHP archive!". However, when I call the myphar.phar from a webbrowser, it prints some weird characters, like ????�???�?, instead of my index.php contents and no error.

I added the following line to my apache httpd.conf to support phar archives:

AddType application/x-httpd-php .phar

Does anybody know why it works on the cli, but not in the browser?

A: 

Have you tried to view the source of the web page? Maybe you have somehow forgot to restart Apache after modifying httpd.conf.

Ionuț G. Stan
The characters I described are the only thing returned in the source. I restarted apache multiple times.
Pascal
Can you offer a download link for the `.phar` file? I want to try it on my machine.
Ionuț G. Stan
Here it is: http://bit.ly/12Xr9CIt's zipped, but not with phar compression.
Pascal
Very weird. It's working fine on my machine. It's a Windows XP SP3. Don't know what else to say.
Ionuț G. Stan
A: 

My understanding of the phar file , you are suppose to open(fopen) the file in your php script which you are gonna be calling....if you are getting those characters back, it means apache is sending you the file in binary format....

if you add this "AddType application/x-gzip .gz .tgz .phar" to the apache config, it will make the file download-able like any other archive file and will not execute it like a php script

Ronald Conco
I don't want to download the file as a binary, but want to display it's contents
Pascal
+1  A: 

Try renaming the file to myphar.phar.php (don't forget to keep the '.phar' in the file name) and then calling it from your browser using that name.

Sometimes phar works only that way...not sure why, if the type is already added to httpd.conf.

Oliver
A: 

The answer to this problem lies in the

detect_unicode

setting in your php.ini.

There is currently a bug in PHP (http://bugs.php.net/bug.php?id=42396) which makes __halt_compiler() not work correctly if detect_unicode is enabled.

So as a temporary workaround, set

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 are still fine inside comments and strings)

pilif