exec decompress tar file

Hi,

I use the following to decompress a tar file, in the same directory:
$filename = "homedir.tar";
exec("tar xvf $filename");

Open in new window


How do I make it so the files will be extracted to a directory instead?
Tried this, but it does not work:

$filename = "homedir.tar";
exec("tar xvf $filename homedir/");

Open in new window

kgp43Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
crazedsanityConnect With a Mentor Commented:
An important part of this is whether or not the tarball contains a subfolder or not.  When I create one, I tar up an entire folder; it is possible, however, to create it with files that would (normally) extract into the same directory as the tarball.

Do you have SSH access to the server?  If so, I would suggest creating an "rw" directory (readable + writable) beneath wherever you have the file:
user@server:~$ cd /path/to/folder
user@server:/path/to/folder$ mkdir rw
user@server:/path/to/folder$ chmod a+rw rw/

Open in new window


Now the PHP script, in a file like "test.php" within the "/path/to/folder/":
<?php

chdir(getcwd() .'/rw/');

$filename = 'test.tgz';
$outputArr = array();
$returnVar = ""; 
$command = 'tar zxvf '. dirname(__FILE__) .'/test.tgz -C '. getcwd();
$output = exec($command, $outputArr, $returnVar);

Open in new window

0
 
PapertripCommented:
Need to add -C

$filename = "homedir.tar";
exec("tar -C /destination xvf $filename");

Open in new window

0
 
kgp43Author Commented:
That extracted the files into the same directory as the code
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
PapertripCommented:
May need to add the trailing slash "/destination/"

pod@box:~$ ls
a  b  c  test.tar
pod@box:~$ mkdir dest
pod@box:~$ tar -C dest/ -xvf test.tar
a
b
c
pod@box:~$ ls dest/
a  b  c



Also -C might not be supported by your version of tar.  Type " tar --help | grep '\-C'

pod@box:~$ tar --help|grep '\-C'
  -C, --directory=DIR        change to directory DIR

You can always try "--directory=DIR" as well
0
 
kgp43Author Commented:
I got the following result:

echo exec("tar --help|grep '\-C'");
returns:
--null -T reads null-terminated names, disable -C
0
 
crazedsanityCommented:
Is this a CLI-based script (i.e. run on the command line) or through a webserver?  The difference is pretty big, as the webserver generally runs as a different user and has different permissions... you are running some form of Linux, right?
0
 
kgp43Author Commented:
its from my PHP coding.
0
 
kgp43Author Commented:
using exec();
0
 
crazedsanityCommented:
Are you going to a webpage to run this script?  Or maybe on a desktop clicking the script to run it? Or do you have a command line like:

user@server:~$ php myfile.php

Open in new window

0
 
kgp43Author Commented:
webpage, on my dedicated server.
0
 
crazedsanityCommented:
Wherever you're going to be decompressing the file, make SURE there is write permission for whatever user owns the server process has permission to access the folder.

On my server, I have an "EE" folder underneath the public_html folder, which is where I have the test script.  I created a "rw" folder beneath that, and set those permissions:
chmod a+rwx rw/

Open in new window


When the script runs, make sure to set the entire path to the tar file.  I put the tar file in the "EE" folder, where my script resides, so I did this:
<?php

chdir(getcwd() .'/rw/');

$filename = 'test.tgz';
$outputArr = array();
$returnVar = ""; 
$command = 'tar zxvf '. dirname(__FILE__) .'/test.tgz -C '. getcwd();
$output = exec($command, $outputArr, $returnVar);

Open in new window

0
 
PapertripCommented:
@crazedsanity
I already mentioned using the -C flag for tar, but his version does not have that.

@kgp43
type 'tar --version' and paste the output

I got the following result:

echo exec("tar --help|grep '\-C'");
returns:
--null -T reads null-terminated names, disable -C

It's weird that you got that --null explanation, especially with a -C at the end, since that is exactly what I get, but along with the -C explanation

pod@box:~$ tar --help |grep '\-C'
  -C, --directory=DIR        change to directory DIR
      --null                 -T reads null-terminated names, disable -C
pod@box:~$

Open in new window


Try 'tar --directory=destination xvf $filename' instead of -C
0
 
crazedsanityCommented:
It would be helpful to get the version as well:
user@lenny:~$ tar --version
tar (GNU tar) 1.22
Copyright (C) 2009 Free Soft...

Open in new window

0
 
kgp43Author Commented:
Update:

$status = array();
exec("tar --directory=homedir xvf $filepath", $status);
print_r($status);

Open in new window

Output: Array ()

echo exec("tar --version");

Open in new window

Output: "Written by John Gilmore and Jay Fenlason."
0
 
kgp43Author Commented:
Doing echo exec("tar --version"); does not return the version number, just a "Written by John Gilmore and Jay Fenlason" text
0
 
crazedsanityCommented:
That's not very helpful.... try this:
<?php
$output = array();
$status = null;
$cmd = "tar --version";
exec($cmd, $output, $status);

print_r($output);

Open in new window

0
 
kgp43Author Commented:
Array (
[0] => tar (GNU tar) 1.23
[1] => Copyright (C) 2010 Free Software Foundation, Inc.
[2] => License GPLv3+: GNU GPL version 3 or later .
[3] => This is free software: you are free to change and redistribute it.
[4] => There is NO WARRANTY, to the extent permitted by law.
[5] =>
[6] => Written by John Gilmore and Jay Fenlason. )
0
 
crazedsanityCommented:
So, to make sure the "-C" option is available (it is on my system, which has v1.22), just change line 4:
$cmd = "tar --help";

Open in new window


That will give us all the available options for the program.  I am confident that the "-C" option is there.  Once that's out of the way, we can move on with fixing the problem.
0
 
kgp43Author Commented:
A lot of content from that --help :)

http://sdftp01.com/test/test/test.php
0
 
crazedsanityCommented:
Indeed there is.  The important part is in index 167:
...
166 -> --backup[=CONTROL] backup before removal, choose version CONTROL
167 -> -C, --directory=DIR change to directory DIR
168 -> --exclude=PATTERN exclude files, given as a PATTERN 
...

Open in new window


So the "-C" option is available, so that possible roadblock is gone.
0
 
crazedsanityCommented:
So can you explain exactly what you're trying to do, in a way that is as detailed as possible?

There are a lot of limitations imposed on PHP for permissions.  If you're trying to overwrite a directory with an old backup of it, you probably won't be able to.  If you're just trying to extract a tarball (creating a new directory), that's a different story.

If we knew what you were trying to do with the info extracted from the tarball, it may shed some light on the problem.  There may be alternative ways to accomplish your goal that would be better, if we had the opportunity to understand that goal.
0
 
kgp43Author Commented:
I have a .tar file which I need to extract into a folder/directory.
The .tar file and the folder is in the same directory.

That's about it.
0
All Courses

From novice to tech pro — start learning today.