Link to home
Start Free TrialLog in
Avatar of kgp43
kgp43Flag for Denmark

asked on

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

Avatar of Papertrip
Papertrip
Flag of United States of America image

Need to add -C

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

Open in new window

Avatar of kgp43

ASKER

That extracted the files into the same directory as the code
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
Avatar of kgp43

ASKER

I got the following result:

echo exec("tar --help|grep '\-C'");
returns:
--null -T reads null-terminated names, disable -C
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?
Avatar of kgp43

ASKER

its from my PHP coding.
Avatar of kgp43

ASKER

using exec();
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

Avatar of kgp43

ASKER

webpage, on my dedicated server.
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

@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
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

Avatar of kgp43

ASKER

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."
Avatar of kgp43

ASKER

Doing echo exec("tar --version"); does not return the version number, just a "Written by John Gilmore and Jay Fenlason" text
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

Avatar of kgp43

ASKER

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. )
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.
Avatar of kgp43

ASKER

A lot of content from that --help :)

http://sdftp01.com/test/test/test.php
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.
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.
Avatar of kgp43

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of crazedsanity
crazedsanity
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial