2, system function
<? php $ test = "ls / tmp / test"; $ last = system ($ test); print "last: $ lastn";?>
Return result:
[Root @ krlcgcms01 shell] # php system.php 1001.log 10.log 10.tar.gz aaa.tar.gz mytest test1101 test1102 weblog_2010_09 last: weblog_2010_09
3, passthru function
<? php $ test = "ls / tmp / test"; passthru ($ test);?>
4, popen function
<? php $ test = "ls / tmp / test"; $ fp = popen ($ test, "r"); // popen hits a process channel while (! feof ($ fp)) {// gets from the channel Something $ out = fgets ($ fp, 4096); echo $ out; // print it out} pclose ($ fp);?>
5, proc_open function
<? php $ test = "ls / tmp / test"; $ arrayarray = array ("pipe", "r"), // standard input array // $ fp = proc_open ($ test, $ array, $ pipes); // open a process channel echo stream_get_contents ($ pipes [1]); // Why $ pipes [1] because 1 is the output proc_close ($ fp);?>
6, shell_exec function
<? php $ test = "ls / tmp / test"; $ out = shell_exec ($ test); echo $ out;?>
The results of popen, passthru, proc_open, shell_exec are as follows:
[Root @ krlcgcms01 shell] # php test.php 1001.log 10.log 10.tar.gz aaa.tar.gz mytest test1101 test1102 weblog_2010_09
I can find these few functions, the implementation of the Linux command, I think there should be, welcome to add.
Under normal circumstances, very few will use php to execute linux commands, but under special circumstances, you may use these functions. I used to know that there are two functions that can execute linux commands, one for exec and one for shell_exec. In fact, there are many, combined with the manual content, introduce the following six functions.
1, exec function
<? php $ test = "ls / tmp / test"; // ls is under linux directory, file command exec ($ test, $ array); // Execute the command print_r ($ array);?>
The result is as follows:
[root @ krlcgcms01 shell] # php ./exec.php Array ([0] => 1001.log [1] => 10.log [2] => 10.tar.gz [3] => aaa.tar. gz [4] => mytest [5] => test1101 [6] => test1102 [7] => weblog_2010_09)