PHP shell tar problem, return code 2

て烟熏妆下的殇ゞ 提交于 2019-12-13 00:35:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!