system() - 执行外部程序,并且显示输出 - php 执行命令函数
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参数,则外部命令执行后的返回状态将会被设置到此变量中。
返回值
成功则返回命令输出的最后一行,失败则返回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
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!