Php external execution command function usage summary, execution command Summary

Source: Internet
Author: User

Php external execution command function usage summary, execution command Summary

This example describes the usage of php external execution command functions. We will share this with you for your reference. The details are as follows:

First of all, you should first review it yourself. It has been more than a year before and after learning and using php. It has been almost a year since you studied and used Linux, I don't know that php, an interpreted language, can directly call operation commands to manipulate the system...
First, we will summarize the Linux operations. We will not mention the commonly used cd, cp, mv, rm, and so on, the first point worth noting is that when a common user extracts the root permission, he must add sudo before the command, and then output the line number command in the vi Editor: set nu.
Php built-in functions exec and system can call system commands (shell commands). However, to use these two functions php. the security mode in ini must be disabled. Otherwise, php will not allow you to call system commands for security reasons.

Let's take a look at the php Manual's explanation of the two functions:

1. exec --- execute external program

Syntax: string exec (string command [, array & output [, int & return_var])

Note:

Exec () executes the command, but it does not output anything. It simply transmits the last line from the command result. If you need to execute a command, you can use the passthru () function to obtain all data from the command.

If the array parameter is given, the specified array will be filled with each line output by the command. Note: If the array already contains some elements, exec () it will be appended to the back of the array. If you do not want this function to append an element, you can call unset () before passing this array to exec ().

2. system --- execute an external program and display the output

Syntax: string system (string command [, int & return_var])

Note:

System () executes the command and outputs the result. If the return_var parameter is given, the status code for executing the command will be written to this variable.

If PHP runs as a server module, after each line is output, system () will try to automatically clear the web server's output buffer. If the command succeeds, the last line of the command is returned. If the command fails, false is returned.

If you need to execute a command and obtain all the information from the command, you can use the passthru () function.

Both are used to call the system shell command,

Differences:

Exec can return all the execution results to the $ output function (array). $ status indicates that the execution status 0 indicates success 1 indicates failure.

Systerm does not need to provide the $ output function. It directly returns the result. Similarly, $ return_var indicates that the execution status code 0 indicates that success 1 indicates failure.

Exec example:

Reference content is as follows:

<?php  $a = exec("dir",$out,$status);  print_r($a);  print_r($out);  print_r($status);?>

System example:

Reference content is as follows:

<?php  $a = system("dir",$out);  print_r($a);  print_r($out);?>

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.