shell_exec在Linux终端上没有运行
尝试运行下面的命令
[root@localhost conf]# shell_exec
-bash: shell_exec: command not found
[root@localhost conf]# shell_exec("pwd")
-bash: syntax error near unexpected token `"pwd"'
[root@localhost conf]#
您试图直接在命令行上执行PHP,而不使用PHP封装,因此bash shell试图执行它,但不识别PHP函数。
您需要在PHP文件中运行shell_exec()
或使用适当的PHP CLI语法。例如,您可以将-r
传递给php
命令,该命令允许您运行代码。
myusername:~$ php -r 'echo shell_exec("pwd");'
/home/myusername
myusername:~$
或者你可以使用内置的REPL。
myusername:~$ php -a
Interactive shell
php > echo shell_exec('pwd');
/home/myusername
php >
还要注意这里使用的echo
,因为成功的结果将返回一个我们想要看到的字符串。