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 ?
7 Answers
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");'
-
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
– Oliboy50Mar 21, 2015 at 7:33 -
4@Oliboy50 Invert the
'
and"
asphp -r "$phar = new Phar('phar-file.phar'); $phar->extractTo('./directory');"
. Probably a Windows-related quotes stuff.– XenosSep 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
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
-
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
tophar
and then the normal command would work.– lyteApr 15, 2019 at 23:25
Yes, this library can do it: https://github.com/koto/phar-util
phar-extract library.phar output-directory
-
1Note 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
PhpStorm IDE can be used for viewing content of phar archives.
-
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 OninDec 7, 2016 at 0:55
-
-
1
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.
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!
-
2To 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