Example of PHP calling Linux external commands

Source: Internet
Author: User
Tags php language


I believe we have more or less used AMH,VESTACP and other VPS panels, these panels are used in the PHP language, is essentially PHP to perform the external command of Linux.

PHP provides a number of functions for executing external commands, including shell_exec (), exec (), PassThru (), and System (). These commands are similar, but provide a different interface for the external programs that you run. All of these commands derive a subprocess to run the command or script that you specify, and each subprocess catches them when the command output is written to standard output (stdout).

shell_exec function

Description: Runs the external program through the shell and returns the result as a string.

Syntax: String shell_exec (String $cmd)

return value: String

Detailed Introduction
The shell_exec () command line is actually only a variant of the anti-apostrophe (') operator, which allows you to run the shell command and then return the result as a string.

Sample code

Count the number of words in all files in the current directory and output the first five elements.


<?php
$results = Shell_exec (' wc-w *.txt | head-5 ');
echo "<pre>". $results. "
“;
?>

exec function

Description: Similar to Shell_exec (), returns the last line of output

Syntax: string exec (String $command [, Array & $output [, int & $return _var]])
return value: String

Detailed Introduction:
This function executes an external program or an external instruction that enters the command. Its return string is only the last line returned after the external program executes, and if the Return_var and output two parameters are present, then the state after executing the command is filled in the Return_var.

Instance code:

Counts the number of words in all files in the current directory and outputs the first five lines, but actually only one row is exported.


<?php
$results = EXEC (' wc-w *.txt | head-5 ');
Echo $results;

#只会输出一行:
#3847 myfile.txt
?>
PassThru ()

Description: PassThru () allows you to run external programs and display the results on the screen.

Syntax: void PassThru (String $command [, int & $return _var])
return value: Integer

Detailed Introduction:
PassThru () allows you to run external programs and display the results on the screen. You do not need to use echo or return to view the results, which are displayed on the browser. You can add optional parameters, that is, a variable that holds the code returned from an external program, such as a successful 0, which provides a better mechanism for debugging.

Instance code:


<?php
PassThru (' wc-w *.txt | head-5 ', $returnval);
echo "?>
system function

Description: Execute external program and display output data.

Syntax: String system (String $command [, int & $return _var])
return value: String

Detailed Introduction
The system () command is a mixed body. It directly outputs anything received from an external program like PassThru (). It also returns the last line like exec () and makes the return code available.

Sample code

<?php
System (' wc-w *.txt | head-5 ');

#输出如下:
#123 file1.txt 332 file2.txt 444 File3.txt
#and so on
?>
Summary

Generally speaking, exec () command is more commonly used;
You can use Shell_exec () If you don't care about the result and the command is simpler.
If you only need to return a shell script, you can use PassThru ().

However, small knitting or to say, there is no need to use PHP to perform system functions, we can be banned, in the php.ini we write as follows

Disable_functions = Proc_open,exec,passthru,shell_exec,system,popen

It's OK.

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.