Removing folder structure when making a tar from a folder

1

Ok I don't really want to waste any more time on investigating this but I do want to offer a .tar option for download instead of just zip.

    $archive = "/home/stevenbu/public_html/temp/".$twitter_name.".tar";
    $directory = "/home/stevenbu/public_html/temp/".$twitter_name;
    exec("tar -cf $archive $directory");

The problem is that when I extract the .tar it is 5 folders deep I would like no folders and just the files in the .tar

Many thanks

StevenBullen

Posted 2011-05-23T13:28:38.983

Reputation: 13

Not really a PHP question, more of a tar question. – AJ. – 2011-05-23T13:31:54.133

and tar doesn't have a 'junk folders' option... – pavium – 2011-05-23T13:37:49.023

While not directly related to your question, always consider the possibility that someone enters $(rm -rf ~) as their Twitter name. – user1686 – 2011-05-23T15:00:09.937

@AJ It is PHP related as it's run from php exec and not directly. Which does make a difference in certain situations.

@grawity Thanks for the heads up, but Twitter would not let you have that name and users must authenticate with twitter to use the script. The twitter name comes from me not from them. – StevenBullen – 2011-05-23T15:13:51.147

Answers

1

It's been a while since I last used php, but

exec("cd /home/stevenbu/public_html/temp/;tar -cf $archive $directory");

would help in shell.

Ahe

Posted 2011-05-23T13:28:38.983

Reputation: 969

This does work but instead I needed to use && instead of ; otherwise it went back to the default directory.

exec("cd /home/stevenbu/public_html/temp/;tar -cf $archive $directory");
 – StevenBullen  – 2011-05-23T15:09:19.567

0

$parent = "/home/stevenbu/public_html/temp";
$dir = $twitter_name;
$archive = $twitter_name.".tgz";

exec('tar -cz -f '.escapeshellarg($archive).' -C '.escapeshellarg($parent).' '.escapeshellarg($dir));

This one is slightly better, because it outputs the .tar.gz file directly to the browser, instead of creating a temporary file:

$parent = "/home/stevenbu/public_html/temp";
$dir = $twitter_name;
$archive = $twitter_name.".tar.gz";

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.addslashes($archive).'"');
passthru('tar -cz -C '.escapeshellarg($parent).' '.escapeshellarg($dir));

user1686

Posted 2011-05-23T13:28:38.983

Reputation: 283 655