Solved

exec decompress tar file

Posted on 2011-09-05
22
378 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
  • 10
  • 9
  • 3
22 Comments
 
LVL 21

Expert Comment

by:Papertrip
Comment Utility
Need to add -C

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

Open in new window

0
 

Author Comment

by:kgp43
Comment Utility
That extracted the files into the same directory as the code
0
 
LVL 21

Expert Comment

by:Papertrip
Comment Utility
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
 

Author Comment

by:kgp43
Comment Utility
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
Comment Utility
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
Comment Utility
its from my PHP coding.
0
 

Author Comment

by:kgp43
Comment Utility
using exec();
0
 
LVL 9

Expert Comment

by:crazedsanity
Comment Utility
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
Comment Utility
webpage, on my dedicated server.
0
 
LVL 9

Expert Comment

by:crazedsanity
Comment Utility
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
Comment Utility
@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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Expert Comment

by:crazedsanity
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
A lot of content from that --help :)

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

Expert Comment

by:crazedsanity
Comment Utility
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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now