Common functions used to execute linux commands in PHP, and phplinux command aggregation _ PHP Tutorial

Source: Internet
Author: User
Common functions and phplinux commands used to execute linux commands in PHP. Common functions used to execute linux commands in PHP are summarized. In general, phplinux commands are rarely used to execute linux commands. However, in special cases, you may use these functions. In the past, I used PHP to execute common functions for linux commands and phplinux commands.

In general, php is rarely used to execute linux commands, but in special cases, you may use these functions. I used to know that there are two functions that can execute linux commands: exec and shell_exec. In fact, there are a lot of functions. the following six functions are introduced in combination with the manual content.

1. exec function

<? Php $ test = "ls/tmp/test"; // ls is the directory for querying files in linux. The Command exec ($ test, $ array) of the file ); // execute the 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 functions

<?php $test = "ls /tmp/test";$last = system($test);print "last: $last\n";?>

Returned results:

[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 plays a process Channel while (! Feof ($ fp) {// get something from the channel $ out = fgets ($ fp, 4096); echo $ out; // Print it out} pclose ($ fp);?>

5. proc_open function

<? Php $ test = "ls/tmp/test"; $ array = array ("pipe", "r"), // standard input array ("pipe ", "w"), // standard output content array ("pipe", "w") // standard output error); $ fp = proc_open ($ test, $ array, $ pipes); // open a process Channel echo stream_get_contents ($ pipes [1]); // why is it $ pipes [1]? because 1 is the output content proc_close ($ fp);?>

6. shell_exec function

<?php $test = "ls /tmp/test";$out = shell_exec($test);echo $out;?>

Popen, passthru, proc_open, and shell_exec return the following results:

[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 out that these functions can execute commands in linux. I think there should be some more.

Articles you may be interested in:
  • Common functions used to execute linux commands in PHP
  • How to support php iconv () functions on linux
  • How to encrypt strings using md5 functions in Linux using PHP scripts

In general, Handler seldom uses php to execute linux commands, but in special cases, you may use these functions. I used...

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.