在 PHP 中执行 shell 脚本时出错
我试图通过以下方式执行 shell 命令:
<php echo shell_execute("tac /home/kusmuk/access-logs/kusmuk.org"); ?>
但它没有给出任何输出。可能是什么原因?
尽管它不起作用,但以下几行可以按预期工作:
<php echo shell_execute("ls -al triogrup.com"); ?>
//outputs: -rw-r----- 2 root kusmuk 28640 Aug 19 17:44 kusmuk.org
<php echo shell_execute("pwd"); ?>
//outputs: /home/kusmuk/public_html
I am trying to execute a shell command via:
<php echo shell_execute("tac /home/kusmuk/access-logs/kusmuk.org"); ?>
But it does not give any output. What could be the reason?
Although it does not work, the following lines work as expected:
<php echo shell_execute("ls -al triogrup.com"); ?>
//outputs: -rw-r----- 2 root kusmuk 28640 Aug 19 17:44 kusmuk.org
<php echo shell_execute("pwd"); ?>
//outputs: /home/kusmuk/public_html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
格雷格的建议很好。您可能最终会遇到某种权限问题。
不过,我想说,如果可能的话,避免从 PHP 启动系统调用是个好主意。调试可能会很痛苦,如果您传递参数,则很容易出现安全漏洞。原生 PHP 代码更容易处理。
'tac' 非常简单,您应该能够在 PHP 中很好地完成它。例如,一个简单的版本可以一次性输出整个文件:
Greg's tip is good. You'll probably end up with some kind of permissions problem.
However, I would say it's a good idea to avoid launching system calls from PHP if possible. The debugging can be a pain and if you're passing parameters it is very easy to make security holes. Native PHP code is much easier to handle.
‘tac’ is simple enough that you should be able to do it fine from within PHP. For example a trival version that spits out the whole file in one go:
试试这个:
它将把 stderr 重定向到 stdout,所以希望你应该明白为什么它不起作用
Try this:
It will redirect stderr to stdout so hopefully you should see why it's not working