PHP Execution System external Command systems () EXEC () PassThru ()

Source: Internet
Author: User

PHP中调用外部命令,可以用如下三种方法来实现:方法一:用PHP提供的专门函数(四个):PHP提供4个专门的执行外部命令的函数:exec(), system(), passthru(), shell_exec()1)exec()原型: string exec( string $command[, array&$output[, int &$return_var]] ) 说明: exec执行系统外部命令时不会输出结果,而是返回结果的最后一行。如果想得到结果,可以使用第二个参数,让其输出到指定的数组。此数组一个记录代表输出的一行。即如果输出结果有20行,则这个数组就有20条记录,所以如果需要反复输出调用不同系统外部命令的结果,最好在输出每一条系统外部命令结果时清空这个数组unset($output),以防混乱。第三个参数用来取得命令执行的状态码,通常执行成功都是返回0。<?php    exec("dir",$output);    print_r($output);?>2)system()原型: string system ( string $command[, int &$return_var ] ) 说明: system和exec的区别在于,system在执行系统外部命令时,直接将结果输出到游览器,如果执行命令成功则返回true,否则返回false。第二个参数与exec第三个参数含义一样。<?phpsystem("pwd");?>3)passthru()原型: void passthru( string $command[, int &$return_var] ) 说明: passthru与system的区别,passthru直接将结果输出到游览器,不返回任何值,且其可以输出二进制,比像数据。第二个参数可选,是状态码。<?phpheader("Content-type:image/gif");passthru("/usr/bin/ppm2tiff  /usr/share/tk8.4/demos/images/teapot.ppm");?>4)shell_exec()原型: string shell_exec ( string $cmd) 说明: 直接执行命令$cmd<?php$output= shell_exec(‘ls -lart‘);echo"<pre>$output</pre>";?> 方法二:反撇号原型: 反撇号`(和~在同一个键)执行系统外部命令 说明: 在使用这种方法执行系统外部命令时,要确保shell_exec函数可用,否则是无法使用这种反撇号执行系统外部命令的。 <?php    echo`dir`;?>方法三:用popen()函数打开进程  原型: resource popen ( string $command, string $mode) 说明: 能够和命令进行交互。之前介绍的方法只能简单地执行命令,却不能与命令交互。有时须向命令输入一些东西,如在增加系统用户时,要调用su来把当前用户换到root用户,而su命令必须要在命令行上输入root的密码。这种情况下,用之前提到的方法显然是不行的。 popen( )函数打开一个进程管道来执行给定的命令,返回一个文件句柄,可以对它读和写。返回值和fopen()函数一样,返回一个文件指针。除非使用的是单一的模式打开(读or写),否则必须使用pclose()函数关闭。该指针可以被fgets(),fgetss(),fwrite()调用。出错时,返回FALSE。<?phperror_reporting(E_ALL); /* Add redirection so we can get stderr. */$handle= popen(‘/path/to/executable 2>&1‘, ‘r‘);echo"‘$handle‘; ". gettype($handle) . "\n";$read = fread($handle, 2096);echo$read;pclose($handle);?>要考虑两个问题:安全性和超时1)安全性由于PHP基本是用于WEB程序开发的,所以安全性成了人们考虑的一个重要方面 。于是PHP的设计者们给PHP加了一个门:安全模式。如果运行在安全模式下,那么PHP脚本中将受 到如下四个方面的限制: 执行外部命令在打开文件时有些限制连接MySQL数据库基于HTTP的认证在安全模式下,只有在特定目录中的外部程序才可以被执行,对其它程序的调用将被拒绝。这个目录可以在PhP.ini 文件中用safe_mode_exec_dir指令,或在编译PHP是加上--with-exec-dir选项来指定。 当你使用这些函数来执行系统命令时,可以使用escapeshellcmd()和escapeshellarg()函数阻止用户恶意在系统上执行命令,escapeshellcmd()针对的是执行的系统命令,而escapeshellarg()针对的是执行系统命令的参数。这两个参数有点类似addslashes()的功能。 2)超时    当执行命令的返回结果非常庞大时,可以需要考虑将返回结果输出至其他文件,再另行读取文件,这样可以显著提高程序执行的效率。如果要执行的命令要花费很长的时间,那么应该把这个命令放到系统的后台去运行。但在默认情况下,象system()等函数要等到这个命令运行完才返回(实际上是在等命令的输出结果),这肯定会引起PHP脚本的超时。解决的办法是把命令的输出重定向到另外一个文件或流中,如:<?phpsystem("/usr/local/bin/order_proc  >  /tmp/abc "); ?>

  

But I call the DOS command takes a few minutes, and in order to batch processing can not simply write the results to the file, order to execute the following program

PHP sets the time limit for invoking system commands , and if the call command times out, the command is still executed, but PHP does not get the return value and is terminated (most hateful, no error is displayed)

Modify php.ini and restart Apache to allow system commands to run for a longer period of time

Max_execution_time = 600

PHP Execution System external Command systems () EXEC () PassThru ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.