问题
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:
Maybe: shell_exec("/bin/bash tar ....")
回答2:
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>";
来源:https://stackoverflow.com/questions/2173443/php-shell-tar-problem-return-code-2