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 ?

epsilones
  • 11,279
  • 21
  • 61
  • 85

7 Answers7

98

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");'
Adrian Heine
  • 4,051
  • 2
  • 30
  • 43
  • 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 '15 at 07: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 08 '15 at 20:10
  • Starting from PHP 5.4 you can even make it shorter: `(new Phar("phar-file.phar"))->extractTo("./directory");` – Gras Double Feb 24 '17 at 03: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 '21 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.

lyte
  • 1,162
  • 10
  • 9
22

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

phar-extract library.phar output-directory

noetix
  • 4,773
  • 3
  • 26
  • 47
  • 1
    Note that it seems not to work well with Phar archives [whose contents were gzipped](http://www.php.net/manual/en/pharfileinfo.compress.php). 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. – Camilo Martin Nov 15 '13 at 21:56
14

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

ya.teck
  • 2,060
  • 28
  • 34
8

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

Try it out.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Najmal Jabbar
  • 305
  • 2
  • 4
  • 9
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.

pozs
  • 34,608
  • 5
  • 57
  • 63
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!

Josef
  • 1,467
  • 2
  • 24
  • 40
  • 2
    To extract readonly phar archives using cli use > php -dphar.readonly=0 extract.php the extract.php contains the code above . – Alin Razvan Jul 05 '19 at 03:33
  • 4
    `php -dphar.readonly=0 -r '(new Phar("archive.phar"))->convertToExecutable(Phar::TAR,Phar::NONE)->extractTo("./output");'` – Alex Barker Feb 02 '20 at 07:57