4

I'm trying to extract a .tar archive by means of PHP. I'm using such a code:

$phar = new PharData('test.tar');
$phar->extractTo('/home/user/newtest');

It works fine, but if my archive contains en empty directory it is not extracted by the mentioned code. So, if my .tar file has this structure:

user@computer:~/test$ tar -tvf test.tar
drwxrwxr-x user/user         0 2012-12-07 22:35 dir1/
-rw-rw-r-- user/user       798 2012-10-28 23:41 dir1/articles.txt
drwxrwxr-x user/user         0 2012-12-07 22:35 emptyDir/
-rw-rw-r-- user/user       834 2012-09-15 22:47 main.html

I get after unpacking:

user@computer:~/newtest$ ls -l
drwxrwxr-x 2 user user 4096 Dec.   7 22:35 dir1
-rw-rw-r-- 1 user user  834 Sep.  15 22:47 main.html

So, the empty 'emptyDir' is not extracted.

Does anyone know how to fix it?

Thank you in advance for any assistance!

scrutari
  • 1,378
  • 2
  • 17
  • 33

1 Answers1

1

It looks like test.tar was created using tar -cf test.tar ... on the command line. PHP's Phar library is meant to read phar archives, not tar archives.

The PHP Manual shows the differences between phar, tar, and zip. Granted, PharData is supposed to work on tar archives, so you might want to report this as a bug to the PHP developers.

For now, if you want to extract a tar archive in PHP, you have three options:

  1. Create the archive using the Phar library in PHP and then extract in PHP
  2. Use Pear's Archive_Tar (which works on tar and tar.gz files).
  3. Call exec("tar -xf test.tar -C /home/user/newtest"); (which is not recommended, but will work)
cegfault
  • 6,442
  • 3
  • 27
  • 49
  • Thank you for your answer. This archive was created with PharData::adfFile and PharData::addEmptyDir functions. Using 'exec' functions is not an option for me as it's not safe. – scrutari Dec 07 '12 at 22:42
  • you really should look int Archive_Tar. PharData is very finicky, and I just don't like it – cegfault Dec 07 '12 at 23:46
  • @cegfault Could you please tell me why is using exec() not recommended? – gen Apr 13 '15 at 08:02
  • also, can someone please confirm if this is a bug? I experience the same phenomena with php 5.3.3. – gen Apr 13 '15 at 08:13
  • @gen: `exec()` has major security concerns, not to mention the loss of portability. You should be able to find a lot on google about it. And seeing how this question and answer was from over two years ago, I can't confirm it's a bug, but if you're experiencing this on php 5.3.3, I suggest you fill out a bug report with PHP and let them diagnose it – cegfault Apr 14 '15 at 21:20
  • @cegfault thank you for your reply; I tried to report the bug, but php seems no longer concerned about this version, so I couldn't eventually submit the report. – gen Apr 15 '15 at 06:48