60

There is something I entirely missed as for phar files. I am installing a project that requires phpunit, pdepend and other dependencies. I fetched them as .phar files. But, I am not able ot extract the files from them using command line tool (php command). I googled the problem, and I found nothing really answering the question. Could anyone help ?

2

7 Answers 7

99

Extending on @pozs’s answer, you can actually use PharData->extractTo in a simple one-liner:

php -r '$phar = new Phar("phar-file.phar"); $phar->extractTo("./directory");'
4
  • 1
    did not work for me on windows PHP 5.6 Parse error: syntax error, unexpected ''$phar' (T_ENCAPSED_AND_WHITESPACE) in Command line code on line 1
    – Oliboy50
    Mar 21, 2015 at 7:33
  • 4
    @Oliboy50 Invert the ' and " as php -r "$phar = new Phar('phar-file.phar'); $phar->extractTo('./directory');". Probably a Windows-related quotes stuff.
    – Xenos
    Sep 8, 2015 at 20:10
  • Starting from PHP 5.4 you can even make it shorter: (new Phar("phar-file.phar"))->extractTo("./directory"); Feb 24, 2017 at 3:02
  • To fix the phar readonly error I did php -d phar.readonly=Off -r '(new Phar("phar-file.phar"))->extractTo("./directory");'
    – Scott P.
    Jan 28, 2021 at 17:31
72

Not sure if it's new, but under PHP 5.4.16 this works:

phar extract -f %some phar file%

The phar is extracted relative to your current working directory.

3
  • 3
    Works in Php 5.3.3 too May 13, 2014 at 17:44
  • 1
    windows php 5.6 needs phar.phar extract -f %some phar file% Oct 10, 2015 at 0:56
  • @Magicianeer that might just be how the phar command is installed in your environment, e.g even in Windows you should be able to rename phar.phar to phar and then the normal command would work.
    – lyte
    Apr 15, 2019 at 23:25
22

Yes, this library can do it: https://github.com/koto/phar-util

phar-extract library.phar output-directory

1
  • 1
    Note that it seems not to work well with Phar archives whose contents were gzipped. At least to me on Windows, but being a development setup, it has gzip, bz2, etc. so it should be able to extract them, but the results are mangled (and don't seem to be even valid as .gz files). The directory structure comes out correctly, though. Nov 15, 2013 at 21:56
14

PhpStorm IDE can be used for viewing content of phar archives.

3
  • 1
    Doesn't work in my case. I'm using the latest current version (2016.3 I believe), and I cannot open a phar file.
    – The Onin
    Dec 7, 2016 at 0:55
  • Sometimes you need to restart PhpStorm to get the file indexed.
    – ya.teck
    Dec 7, 2016 at 5:35
  • 1
    Tried that as well, to no avail.
    – The Onin
    Dec 8, 2016 at 23:46
8

This site converts .phar files to .zip files easily.

Try it out.

4

If you want to just using it, you should include as phar:///path/to/myphar.phar/file.php.

But if you really want to unpack it, see the PharData class - no known (internal) extraction in command line, but you can write a script for that.

3

PHP also has functions for extracting phar archives, but the files keep the current compression. To properly extract an archive it has to be converted into a uncompressed form first and then extracted:

<?php
$phar = new Phar('Someclass.phar');
$phar2 = $phar->convertToExecutable (Phar::TAR,Phar::NONE); // Convert to an uncompressed tar archive
$phar2->extractTo('/some/path/'); // Extract all files

This will give you all the files uncompressed!

2
  • 2
    To extract readonly phar archives using cli use > php -dphar.readonly=0 extract.php the extract.php contains the code above . Jul 5, 2019 at 3:33
  • 4
    php -dphar.readonly=0 -r '(new Phar("archive.phar"))->convertToExecutable(Phar::TAR,Phar::NONE)->extractTo("./output");' Feb 2, 2020 at 7:57

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.