Heim >Backend-Entwicklung >PHP-Tutorial >PHP执行Linux命令常用的6个函数_PHP教程
一般情况下,很少会用php去执行linux命令,不过特殊情况下,你也许会用到这些函数。以前我知道有二个函数可以执行linux命令,一个是exec,一个是shell_exec。其实有很多的,结合手册内容,介绍以下6个函数。
1,exec函数
<ol class="dp-xml"> <li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span> </span></span></li> <li> <span>$</span><span class="attribute">test</span><span> = </span><span class="attribute-value">"ls /tmp/test"</span><span>; //ls是linux下的查目录,文件的命令 </span> </li> <li class="alt"><span>exec($test,$array); //执行命令 </span></li> <li><span>print_r($array); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
返回结果如下:
<ol class="dp-xml"> <li class="alt"><span><span>[root@krlcgcms01 shell]# php ./exec.php </span></span></li> <li><span>Array </span></li> <li class="alt"><span>( </span></li> <li> <span> [0] =</span><span class="tag">></span><span> 1001.log </span> </li> <li class="alt"> <span> [1] =</span><span class="tag">></span><span> 10.log </span> </li> <li> <span> [2] =</span><span class="tag">></span><span> 10.tar.gz </span> </li> <li class="alt"> <span> [3] =</span><span class="tag">></span><span> aaa.tar.gz </span> </li> <li> <span> [4] =</span><span class="tag">></span><span> mytest </span> </li> <li class="alt"> <span> [5] =</span><span class="tag">></span><span> test1101 </span> </li> <li> <span> [6] =</span><span class="tag">></span><span> test1102 </span> </li> <li class="alt"> <span> [7] =</span><span class="tag">></span><span> weblog_2010_09 </span> </li> <li><span>) </span></li> </ol>