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 ?
Asked
Active
Viewed 7.3k times
60
-
1Why would you want to extract them? You can use them directly as phar, see: http://sebastian-bergmann.de/archives/924-Using-PHPUnit-from-a-PHP-Archive-PHAR.html – Dennis Haarbrink Oct 21 '12 at 11:33
-
3The link in the comment above goes to a 404 not found page. – Kmeixner Apr 28 '15 at 19:46
7 Answers
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
-
1did 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
-
3
-
1
-
@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 '19 at 23:25
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
-
1Note 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
-
1Doesn'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 07 '16 at 00:55
-
-
1
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
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
-
2To 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