<>PHP调用shell命令

<>前言

因为想用php调用python脚本,于是想到可以利用php调用shell命令的方法实现。
php版本:php7.0

<>三种方法

<>1.system

system — 执行外部程序,并且显示输出

函数使用方法
system ( string $command [, int &$return_var ] ) : string
example:
<?php $last_line = system('ls',$return_val); echo(“last line:”.$last_line);
echo(“return:”.$return_val);
说明:执行了shell命令,并直接输出其返回结果;
函数的返回结果为shell命令输出内容的最后一行;
$return_val为shell命令的返回结果。

<>2.exec

exec — 执行一个外部程序
exec ( string $command [, array &$output [, int &$return_var ]] ) : string
example:
<?php exec('ls',$out); var_dump($out);
说明:exec函数返回结果为shell命令输出内容的最后一行;
$output为shell命令输出内容;
$return_val为shell命令返回结果;

<>3.passthru

passthru — 执行外部程序并且显示原始输出
passthru ( string $command [, int &$return_var ] ) : void
example:
<?php passthru('ls',$res); echo($res);
说明:当所执行的 Unix 命令输出二进制数据, 并且需要直接传送到浏览器的时候, 需要用此函数来替代 exec() 或 system() 函数。通过设置
Content-type 为 image/gif, 然后调用 pbmplus 程序输出 gif 文件, 就可以从 PHP 脚本中直接输出图像到浏览器

技术
今日推荐
PPT
阅读数 121
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信