PHP执行系统命令的有几个常用的函数,如有:system函数、exec函数、popen函数,passthru,shell_exec函数他们都可以执行系统命令,不过前提时必须系统给了权限了哦。
system函数
说明:执行外部程序并显示输出资料。
语法:string system(string command, int [return_var]);
返回值: 字符串
详细介绍:本函数就像是 C 语中的函数 system(),用来执行指令,并输出结果,若是 return_var 参数存在,则执行 command 之后的状态会填入 return_var 中,同样值得注意的是若需要处理用户输入的资料,而又要防止用户耍花招破解系统,则可以使用 EscapeShellCmd(),若 PHP 以模块式的执行,本函数会在每一行输出后自动更新 Web 服务器的输出缓冲暂存区,若需要完整的返回字符串,且不想经过不必要的其它中间的输出界面,可以使用 PassThru()。
实例代码:
- <?php
- $last_line = system("ls", $retval);
- echo "Last line of the output: " . $last_line;
- echo "<hr />Return value: " . $retval;
- ?>
exec函数
说明:执行外部程序。
语法:string exec(string command, string [array], int [return_var]);
返回值: 字符串
详细介绍:本函数执行输入 command 的外部程序或外部指令。它的返回字符串只是外部程序执行后返回的最后一行;若需要完整的返回字符串,可以使用 PassThru() 这个函数。
要是参数 array 存在,command 会将 array 加到参数中执行,若不欲 array 被处理,可以在执行 exec() 之前呼叫 unset()。若是 return_var 跟 array 二个参数都存在,则执行 command 之后的状态会填入 return_var 中。
值得注意的是若需要处理使用者输入的资料,而又要防止使用者耍花招破解系统,则可以使用 EscapeShellCmd()。
实例代码:
- < ?php
- echo exec("whoami");
- ?>
popen函数
说明:打开文件。
语法:int popen(string command, string mode);
返回值: 整数
详细介绍:本函数执行指令开档,而该文件是用管道方式处理的文件。用本函数打开的文件只能是单向的 (只能读或只能写),而且一定要用 pclose() 关闭。在文件操作上可使用 fgets()、fgetss() 与 fputs()。若是开档发生错误,返回 false 值。
实例代码:
- < ?
- $fp = popen( "/bin/ls", "r" );
- ?>
使用函数passthru执行系统外部命令
原型:function passthru(string $command,int[optional] $return_value)
知识点:passthru与system的区别,passthru直接将结果输出到游览器,不返回任何值,且其可以输出二进制,比如图像数据。
反撇号`(和~在同一个键)执行系统外部命令,代码如下:echo `dir`;
知识点:在使用这种方法执行系统外部命令时,你要确保shell_exec函数可用,否则是无法使用这种反撇号执行系统外部命令的。
安全性说明:当你使用这些函数执行命令时,如果是根据用户提交数据作为执行命令的话,你需要考虑系统安全性,可以使用escapeshellcmd()和escapeshellarg()函数阻止用户恶意在系统上执行命令,escapeshellcmd()针对的是执行的系统命令,而escapeshellarg()针对的是执行系统命令的参数。这两个参数有点类似addslashes()的功能。
现在来看一个自定的执行系统命令的函数,代码如下:
- function execute($cfe) {
- $res = ”;
- if ($cfe) {
- if(function_exists(‘system’)) {
- @ob_start();
- @system($cfe);
- $res = @ob_get_contents();
- @ob_end_clean();
- } elseif(function_exists(‘passthru’)) {
- @ob_start();
- @passthru($cfe);
- $res = @ob_get_contents();
- @ob_end_clean();
- } elseif(function_exists(‘shell_exec’)) {
- $res = @shell_exec($cfe);
- } elseif(function_exists(‘exec’)) {
- @exec($cfe,$res);
- $res = join(“n”,$res);
- } elseif(@is_resource($f = @popen($cfe,”r”))) {
- $res = ”;
- while(!@feof($f)) {
- $res .= @fread($f,1024);
- }
- @pclose($f);
- }
- }
- return $res;
- }
波比源码 » PHP执行系统命令的有几个常用的函数
buy levaquin 500mg levofloxacin online order
oral lamotrigine buy vermox for sale tretinoin gel cost
pharmacie en ligne tadalafil 20mg acheter 50mg du viagra sildenafil 100mg pas cher
doxycycline cheap lasix over the counter buy lasix 40mg
buy clonidine minocin canada purchase spiriva pill
zyban 150mg oral buy seroquel pill seroquel 100mg usa
dapsone 100 mg oral fexofenadine 120mg pills aceon for sale
order medroxyprogesterone for sale hydrochlorothiazide 25 mg us buy cyproheptadine generic
order generic modafinil buy stromectol 3mg pill ivermectin iv
purchase piracetam online cheap sildenafil australia viagra 100mg sale
order sildenafil 50mg sale pfizer viagra 50mg cheap lisinopril 2.5mg
cialis order goodrx cialis purchase plavix
order ranitidine 300mg pills zantac 300mg usa celebrex 200mg over the counter
tamsulosin 0.2mg cheap cost tamsulosin spironolactone 25mg us
diflucan 100mg usa buy viagra 50mg pills generic viagra 50mg
oral cialis 20mg cialis 40mg over the counter sildenafil overnight shipping usa
fildena 50mg price nolvadex 10mg without prescription rhinocort price
order deltasone 20mg without prescription mebendazole pill order mebendazole pills