views:

224

answers:

2

Want to archive a folder using tar from PHP:

$result = shell_exec("tar cf $sourceFile $sourceFolder -C $source  > /dev/null; echo $?");
var_dump($result);

Output:

string(2) "2 "

the > /dev/null; echo $? thing is for outputing the result code of a script under linux;

the -C $source - changes to the right folder before doing anything

This is really strange because when i run this from linux console, it works just fine - creates the archive, so it's not a permission issue.

Other sripts like "whoami" or "ls" work fine.

Any ideas what does it mean?

+1  A: 

Maybe: shell_exec("/bin/bash tar ....")

tur1ng
A: 

Just for debugging purposes redirect stderr to stdout and use passthru() to display the complete output (possibly including error messages).

$cmd = sprintf("tar cf %s %s -C %s 2>&1",
  escapeshellarg($sourceFile),
  escapeshellarg($sourceFolder),
  escapeshellarg($source)
);

echo '<pre>', htmlspecialchars($cmd), ": \n";
flush();
passthru($cmd, $code);
var_dump($code);
echo "</pre>";
VolkerK