失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > apache php 执行shell PHP执行普通shell命令流程解析

apache php 执行shell PHP执行普通shell命令流程解析

时间:2023-12-01 15:21:54

相关推荐

apache php 执行shell PHP执行普通shell命令流程解析

这里演示一些普通的shell命令

php执行shell命令,可以使用下面几个函数:

string system ( string $command [, int &$return_var ] )

string exec ( string $command [, array &$output [, int &$return_var ]] )

void passthru ( string $command [, int &$return_var ] )

注意的是:这三个函数在默认的情况下,都是被禁止了的,如果要使用这几个函数,就要先修改php的配置文件php.ini,查找关键字disable_functions,将这一项中的这几个函数名删除掉,然后注意重启apache。

首先看一下system()和passthru()两个功能类似,可以互换:

$shell = "ls -la";

echo "

";

system($shell, $status);

echo "

";

//注意shell命令的执行结果和执行返回的状态值的对应关系

$shell = "$shell";

if( $status ){

echo "shell命令{$shell}执行失败";

} else {

echo "shell命令{$shell}成功执行";

}

?>

执行结果如下:

注意,system()会将shell命令执行之后,立马显示结果,这一点会比较不方便,因为我们有时候不需要结果立马输出,甚至不需要输出,于是可以用到exec()

exec()的使用示例:

$shell = "ls -la";

exec($shell, $result, $status);

$shell = "$shell";

echo "

";

if( $status ){

echo "shell命令{$shell}执行失败";

} else {

echo "shell命令{$shell}成功执行, 结果如下

";

print_r( $result );

}

echo "

";

?>

运行结果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

如果觉得《apache php 执行shell PHP执行普通shell命令流程解析》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。