kgp43
asked on
exec decompress tar file
Hi,
I use the following to decompress a tar file, in the same directory:
How do I make it so the files will be extracted to a directory instead?
Tried this, but it does not work:
I use the following to decompress a tar file, in the same directory:
$filename = "homedir.tar";
exec("tar xvf $filename");
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/");
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
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
ASKER
I got the following result:
echo exec("tar --help|grep '\-C'");
returns:
--null -T reads null-terminated names, disable -C
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?
ASKER
its from my PHP coding.
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
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:
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:
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/
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);
@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
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
Try 'tar --directory=destination xvf $filename' instead of -C
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:~$
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...
ASKER
Update:
$status = array();
exec("tar --directory=homedir xvf $filepath", $status);
print_r($status);
Output: Array ()echo exec("tar --version");
Output: "Written by John Gilmore and Jay Fenlason."
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);
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. )
[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:
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.
$cmd = "tar --help";
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.
ASKER
Indeed there is. The important part is in index 167:
So the "-C" option is available, so that possible roadblock is gone.
...
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
...
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.
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.
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.
The .tar file and the folder is in the same directory.
That's about it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window