百科狗-知识改变命运!
--

system() - 执行外部程序,并且显示输出 - php 执行命令函数

乐乐1年前 (2023-11-21)阅读数 22#技术干货
文章标签命令

system()

(PHP 4, PHP 5, PHP 7)

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

说明

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

同 C 版本的system()函数一样,本函数执行$command参数所指定的命令,并且输出执行结果。

如果 PHP 运行在服务器模块中,system()函数还会尝试在每行输出完毕之后,自动刷新 web 服务器的输出缓存。

如果要获取一个命令未经任何处理的原始输出,请使用passthru()函数。

参数

$command

要执行的命令。

$return_var

如果提供$return_var参数,则外部命令执行后的返回状态将会被设置到此变量中。

返回值

system() - 执行外部程序,并且显示输出 - php 执行命令函数

成功则返回命令输出的最后一行,失败则返回FALSE

范例

Example #1system()例程

注释

Warning

当用户提供的数据传入此函数,使用escapeshellarg()或escapeshellcmd()来确保用户欺骗系统从而执行任意命令。

Note:

如何程序使用此函数启动,为了能保持在后台运行,此程序必须将输出重定向到文件或其它输出流。否则会导致PHP 挂起,直至程序执行结束。

Note:安全模式启用时,可仅可用safe_mode_exec_dir执行文件。实际上,现在不允许在到可执行的路径中存在..组件。

Warning

安全模式启用时,命令字符串会被escapeshellcmd()转换。因此,echo y | echo x会变成echo y | echo x

参见

  • exec() 执行一个外部程序
  • passthru() 执行外部程序并且显示原始输出
  • popen() 打开进程文件指针
  • escapeshellcmd()shell 元字符转义
  • pcntl_exec() 在当前进程空间执行指定程序
  • 执行操作符
This is for WINDOWS users. I am running apache and I have been trying for hours now to capture the output of a command. 
I'd tried everything that is written here and then continued searching online with no luck at all. The output of the command was never captured. All I got was an empty array.
Finally, I found a comment in a blog by a certain amazing guy that solved my problems. 
Adding the string ' 2>&1' to the command name finally returned the output!! This works in exec() as well as system() in PHP since it uses stream redirection to redirect the output to the correct place!
system("yourCommandName 2>&1",$output) ;
How to produce a system beep with PHP.

This will not do anything when running through a browser, if running through a shell it will produce an audible beep $int_beeps times. This should work on Windows, Unix, etc.
It's important to note that if you are running a series of system() commands in a loop, you need to include the second argument in order for them to run synchonously.
ie)
// this will execute process.php asynchronously; not waiting for completion before executing the next one.
$array = array('apple', 'banana', 'pear');
foreach($array as $i)
{
system("php process.php \"fruit=$i\"");
}
// this will execute process.php 3 times, waiting for the prior command to complete before starting a new one
$array = array('apple', 'banana', 'pear');
foreach($array as $i)
{
system("php process.php \"fruit=$i\"", $status);
}
The double quote problem on Windows platform discussed earlier here in comments still present in PHP 5.2.5. You can't execute system($command) if $command contains more than 2 " chars. system('"echo" A'); works, while system('"echo" "A"'); does not. Search comment, that was a solution posted to overhaul this issue via temporary .bat file.
For PHP running inside a Webserver:
When calling a process via
system("your_process &");
to make it running in background, note that this process is killed when the webserver is restarted.
If you can't see any output or error from system(), shell_exec() etc, you could try this:

For example, "echo shell_exec('ls');" will get nothing output,
"my_exec('ls');" will get "sh: ls: command not found",
"my_exec('/bin/ls');" will maybe get "sh: /bin/ls: Permission denied",
and the permission may be caused by selinux.
another reason to use shell_exec instead of system is when the result is multiple lines such as grep or ls 

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)