Extract tar archive with empty directories with php PharData

I am trying to extract a .tar archive using PHP. I am using code like this:

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

It works fine, but if my archive contains an empty directory , it is not extracted by the specified 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 retrieved .

Does anyone know how to fix this?

Thank you in advance for your help!

+4
source share
1 answer

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

The PHP manual shows the differences between phar, tar and zip. Of course, PharData should work with tar archives, so you can report this as an error to PHP developers.

Now, if you want to extract the tar archive in PHP, you have three options:

  • Create an archive using the Phar library in PHP and then extract in PHP
  • Use Pear Archive_Tar (which works with tar and tar.gz files).
  • Call exec("tar -xf test.tar -C /home/user/newtest"); (which is not recommended, but will work)
+1
source

All Articles