[root@dywang script]# cd tmp/ [root@dywang tmp]# cp /etc/host.conf . [root@dywang tmp]# cat /etc/host.conf multi on order hosts,bind
shell_exe():回傳結果為字串。
[root@dywang tmp]# vim shexe.php
[root@dywang tmp]# cat shexe.php
<?php
$results = shell_exec('cat -n host.conf');
echo $results;
?>
[root@dywang tmp]# php shexe.php
1 multi on
2 order hosts,bind
[root@dywang tmp]# cat -n host.conf
1 multi on
2 order hosts,bind
exe,與 shell_exec() 相似,不同的只回傳最後一行。
[root@dywang tmp]# vim exe.php
[root@dywang tmp]# cat exe.php
<?php
$results = exec('cat -n host.conf');
echo $results;
?>
[root@dywang tmp]# php exe.php
2 order hosts,bind[root@dywang tmp]#
exe,也可以將輸出存成字串陣列,每行為陣列中的一個元素。
[root@dywang tmp]# vim exearr.php
[root@dywang tmp]# cat exearr.php
<?php
exec('cat -n host.conf',$results);
print_r($results);
?>
[root@dywang tmp]# php exearr.php
Array
(
[0] => 1 multi on
[1] => 2 order hosts,bind
)
system() 指令是一種混合體,可以像 shell_exec() 一樣輸出字串,但不必經 echo 輸出到螢幕,而是直接輸出到螢幕。
[root@dywang tmp]# vim system.php
[root@dywang tmp]# cat system.php
<?php
system('cat -n host.conf');
?>
[root@dywang tmp]# php system.php
1 multi on
2 order hosts,bind