Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

exec decompress tar file

Posted on 2011-09-05
22
Medium Priority
?
402 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:kgp43
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 9
  • 3
22 Comments
 
LVL 21

Expert Comment

by:Papertrip
ID: 36485100
Need to add -C

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

Open in new window

0
 

Author Comment

by:kgp43
ID: 36485106
That extracted the files into the same directory as the code
0
 
LVL 21

Expert Comment

by:Papertrip
ID: 36485133
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:kgp43
ID: 36488605
I got the following result:

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

Expert Comment

by:crazedsanity
ID: 36488607
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
 

Author Comment

by:kgp43
ID: 36488729
its from my PHP coding.
0
 

Author Comment

by:kgp43
ID: 36488731
using exec();
0
 
LVL 9

Expert Comment

by:crazedsanity
ID: 36488791
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
 

Author Comment

by:kgp43
ID: 36488856
webpage, on my dedicated server.
0
 
LVL 9

Expert Comment

by:crazedsanity
ID: 36489159
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
 
LVL 21

Expert Comment

by:Papertrip
ID: 36500238
@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
 
LVL 9

Expert Comment

by:crazedsanity
ID: 36502595
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
 

Author Comment

by:kgp43
ID: 36502674
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
 

Author Comment

by:kgp43
ID: 36502679
Doing echo exec("tar --version"); does not return the version number, just a "Written by John Gilmore and Jay Fenlason" text
0
 
LVL 9

Expert Comment

by:crazedsanity
ID: 36502704
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
 

Author Comment

by:kgp43
ID: 36502900
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
 
LVL 9

Expert Comment

by:crazedsanity
ID: 36503092
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
 

Author Comment

by:kgp43
ID: 36503228
A lot of content from that --help :)

http://sdftp01.com/test/test/test.php
0
 
LVL 9

Expert Comment

by:crazedsanity
ID: 36503518
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
 
LVL 9

Expert Comment

by:crazedsanity
ID: 36503548
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
 

Author Comment

by:kgp43
ID: 36503714
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
 
LVL 9

Accepted Solution

by:
crazedsanity earned 2000 total points
ID: 36503831
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question