php - How to create a Phar archive without index?

566

I'm trying to make a Phar archive with one of my lib. The lib is just a bunch of classes organized into folders and subfolders. No index.php at all here, just a static Config class to call to initiate the autoloader.

Anyway, I built a archive like this :

$phar = new Phar(__DIR__ . '/lis.phar',0,'lib.phar');
$phar->buildFromDirectory(__DIR__ . '/class','/\.php$');
$phar->stopBuffering();

After that I'm trying to use the phar like this :

require('lib.phar');
Config::register(); // Config is in the phar

But I get the following error :

Warning: include(phar://D:\wamp\www_test\phar\lib.phar/index.php) [function.include]: failed to open stream: phar error: "index.php" is not a file in phar "D:/wamp/www/_test/phar/lib.phar" in D:\wamp\www_test\phar\lib.phar on line 9

How can I make a phar archive without any index.php file inside it ? In fact I just need the archive to be a container for my files, no need to auto execute anything.

649

Answer

Solution:

First of all, i think you have tostartBuffering() beforestopBuffering(). And I might think thatbuildFromDirectory does this internally for you. You don't need to dostopBuffering() for "sealing" the archive. Its ready "on the fly".

So second: You can watch the defaultStub (which is used in your code implicity) like this:

$phar->setDefaultStub();
var_dump($phar->getStub());

Its a littly bit cryptic, but you will figure it out. It does check for phar stream wrapper support (in 5.3) and if not it extracts the contents to temp file and then executes thePhar::START constant File - which is by default "index.php". And of course it doesPhar::interceptFileFuncs() and sets the include path, which makes the phar working "magic". But your question sounds like you only need an archive for your libs. So you're better off with using the "PharData" class. Haven't tried it yet, but the documentation says so.

People are also looking for solutions to the problem: php - Error when converting a web.config to .htaccess in rewrite

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.