PHP External execution Command function usage Summary code example

Source: Internet
Author: User
This article mainly introduces the PHP external execution command function usage, combined with the instance form analysis exec and the system executes the external command the related use skill, needs the friend can refer to the next

This example describes the use of external command functions for PHP. Share to everyone for your reference, as follows:

First of all to self-review, learning and using PHP has been back and forth for more than a year, research and use of the Linux system for almost one years, I actually do not know that the PHP interpretation of the language can directly invoke the operation command to manipulate the system ...
First summarize the operation of Linux, commonly used CD, CP, MV, RM and so on do not mention, it is worth summing up the 1th is the ordinary user in the root permission when the need to add sudo before the command, and then the VI editor output line number of the command is: Set Nu.
PHP's built-in function Exec,system can call system commands (shell commands), but to use these two functions php.ini in the security mode must be closed, or for security purposes, PHP is not allowed to invoke the system command.

Let's take a look at the explanations of these two functions in the PHP manual:

1. EXEC---Execute external program

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

Description

EXEC () Executes the command given, but it does not output anything, it simply returns the last line from the result of the command, and if you need to execute a command and get all the data from the command, you can use the PassThru () function.

If a parameter array is given, the specified array will be filled with each line output by the command, note that if the array already contains some elements, exec () will append it to the array, and if you do not want this function to append elements, you can pass this array to exec () Before calling Unset ().

2. System---Execute external program and display output

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

Description

System () executes the command given and outputs the result. If a parameter return_var is given, the execution of the command's status code will be written to this variable.

If PHP is operating as a server module, after each line is output, system () tries to automatically clear the output buffer of the Web server. Success returns the last line of the command, which returns false if it fails.

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

These two are all used to invoke the system shell command,

Different points:

Exec can return all execution results to the $output function (array), $status is the execution status 0 for success 1 for failure

Systerm does not need to provide the $output function, he is directly returning the result, the same $return_var is the execution of the status code 0 for the success of 1 for the failure

exec Example:

The following is the referenced content:

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

System Example:

The following is the referenced content:

<?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.