PHP execute Linux Command common function summary _php instance

Source: Internet
Author: User

In general, PHP is rarely used to execute Linux commands, but you might use them in special cases. I used to know that there are two functions that can execute Linux commands, one is exec, the other is shell_exec. In fact, there are many, combined with the contents of the manual, introduced the following 6 functions.

1,exec function

<?php 
$test = "Ls/tmp/test";//ls is the directory under Linux, the command
exec ($test, $array);//execute Command
print_r ($array);
? >

The returned results are 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 
)

2,system function

<?php 
$test = "Ls/tmp/test";
$last = System ($test);
Print "Last: $last \ n";
? >

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 play a process channel while

(!feof ($fp)) {//get something from the channel
$out = fgets ($fp, 4096);
Echo $out; Print out
} 
pclose ($fp);
? >

5,proc_open function

<?php 
$test = "Ls/tmp/test";
$array = Array (
array ("Pipe", "R"),/Standard input 
Array ("Pipe", "w"),//Standard output 
Array ("Pipe", "w")/Standard output error 
); 
$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 the popen,passthru,proc_open,shell_exec return 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 on these several functions, can carry out the commands under Linux, I think there should be, welcome to add.

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.