Home Backend Development PHP Tutorial About how to use exec, system and other functions to call system commands in PHP

About how to use exec, system and other functions to call system commands in PHP

Jun 21, 2018 pm 02:55 PM
exec system

This article mainly introduces the method of using exec, system and other functions to call system commands in PHP. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

PHP AS A server-side scripting language, it is fully capable of tasks such as writing simple or complex dynamic web pages. But things are not always like this. Sometimes in order to implement a certain function, you must resort to external programs (or commands) of the operating system. In this way, you can get twice the result with half the effort.

php's built-in functions exec and system can be used Call system commands (shell commands), and of course passthru, escapeshellcmd and other functions.

In many cases, using PHP's exec, system and other functions to call system commands can help us complete our work better and faster. For example, exec helped me a lot when I was batch processing .rar files two days ago.

Today I will sort out the commonly used calling system functions and share my experience with everyone.

Note: If you want to use these two functions, the safe mode in php.ini must be turned off, otherwise PHP will not allow you to call system commands for security reasons.

First take a look at the explanation of these two functions in the PHP manual:

exec --- Execute external programs

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

 Explanation:

exec() executes the given command command, but it does not output anything, it simply takes the command The last line is returned in the result. If you need to execute a command and get all the data from the command, you can use the passthru() function.

If the parameter array is given, the specified array will be filled with each line output by the command. Note: If the array already contains some elements, exec() will append it to the array. later, if you don't want this function to append elements, you can call unset() before passing the array to exec().

If the parameters array and return_var are given, the status command returned to execution will be written to this variable.

Note: If you allow data from user input to be passed to this function, then you should use escapeshellcmd() to ensure that the user cannot trick the system into executing arbitrary commands. .

Note: If you use this function to start a program and want to leave it while it is running in the background, you must make sure that the output of the program is redirected to a file or some other Output data stream, otherwise PHP will hang until the program execution ends.

 system --- Execute external programs and display output

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

Description:

system() executes the given command command and outputs the result. If the parameter return_var is given, the status code of the executed command will be written to this variable.

Note: If you allow data from user input to be passed to this function, then you should use escapeshellcmd() to ensure that the user cannot trick the system into executing arbitrary commands. .

Note: If you use this function to start a program and want to leave it while it is running in the background, you must make sure that the output of the program is redirected to a file or some other Output data stream, otherwise PHP will hang until the program execution ends.

If PHP is running as a server module, system() will try to automatically clear the web server's output buffer after outputting each line.

Returns the last line of the command if successful, false if failed.

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

Both of these are used to call system shell commands.

The difference is:

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

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

exec example:

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

system example:

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

System, exce, passthru difference
system() Output and return the last line of shell results.
exec() does not output results and returns the last line of shell results. All results can be saved in a returned array.
passthru() only calls the command and outputs the command execution result directly to the standard output device as is.
Same points: you can get the status code of command execution

The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website!

related suggestion:

About the preg_replace() method of regular replacement of all strings that meet the conditions in PHP

Analysis of the filter_var() function and Filter function in PHP

Analysis on str_replace replacement vulnerability in php

The above is the detailed content of About how to use exec, system and other functions to call system commands in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What to do if php exec cannot be executed What to do if php exec cannot be executed Nov 22, 2022 am 10:16 AM

What to do if php exec cannot be executed

Java documentation interpretation: Usage analysis of the currentTimeMillis() method of the System class Java documentation interpretation: Usage analysis of the currentTimeMillis() method of the System class Nov 03, 2023 am 09:30 AM

Java documentation interpretation: Usage analysis of the currentTimeMillis() method of the System class

What to do if php exec results are garbled What to do if php exec results are garbled Oct 21, 2022 am 10:17 AM

What to do if php exec results are garbled

What is the computer system? What is the computer system? Feb 22, 2023 am 10:25 AM

What is the computer system?

Win10 blue screen error: System service exception Win10 blue screen error: System service exception Dec 29, 2023 pm 04:04 PM

Win10 blue screen error: System service exception

Linux exec command manual Linux exec command manual Feb 18, 2024 pm 04:42 PM

Linux exec command manual

Microsoft announces general availability of System Center 2022 Microsoft announces general availability of System Center 2022 Apr 14, 2023 am 09:40 AM

Microsoft announces general availability of System Center 2022

How to use Object class and System class in Java? How to use Object class and System class in Java? Apr 23, 2023 pm 11:28 PM

How to use Object class and System class in Java?

See all articles