


About how to use exec, system and other functions to call system commands in PHP
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); ?>
system example:
<?php $a = system("dir",$out); print_r($a); print_r($out); ?>
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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to the problem that php exec cannot be executed: 1. Find the php configuration file php.ini, search for "disable_functions" in the file, and remove "exec" and "shell_exec"; 2. Go to the server to modify the file permissions; 3. Modify the configuration The safe mode is "safe_mode=On".

Java document interpretation: Usage analysis of the currentTimeMillis() method of the System class, specific code examples are required. In Java programming, the System class is a very important class, which encapsulates some properties and operations related to the system. Among them, the currentTimeMillis method is a very commonly used method in the System class. This article will explain the method in detail and provide code examples. 1. Overview of currentTimeMillis method

The win10 system is a very easy-to-use and highly intelligent system. Its strong compatibility can ensure that the system will basically not have any problems during normal use. However, as people continue to use the win10 system, sometimes the system will also have problems. The problem of Win10 booting blue screen termination code SystemServiceException. Today I will bring you the solution to Win10 booting Blue Screen termination code SystemServiceException. If you need it, please download it quickly. Solution to win10systemserviceexception blue screen: Method 1: 1. Use Windows key + R to open Run and enter “contr

Solution to garbled php exec results: 1. Open the corresponding PHP code file; 2. View the code at exec; 3. Modify the content to "exec("python cmd.py",$str); foreach($str as $ res){$str = iconv("GBK", "UTF-8", $res);}echo $str;".

The computer system is a relatively common system process. You will often see system when viewing the process. This process simply means the computer system; however, if the system.exe process appears on the computer, it needs to be deleted in time. , this is a file generated by a Trojan horse virus. There is no exe suffix behind the real system.

Object is the base class of all Java classes, the top of the entire class inheritance structure, and the most abstract class. Everyone uses toString(), equals(), hashCode(), wait(), notify(), getClass() and other methods every day. Maybe they don’t realize that they are methods of Object, and they don’t look at what other methods Object has. And think about why these methods should be placed in Object. 1. Introduction to JavaObject class - the super class of all classes Object is a special class in the Java class library and is also the parent class of all classes. In other words, Java allows any type of object to be assigned to the Object type

Function Description The exec command is used in Linux to call and execute specified commands. The exec command is usually used in shell scripts to execute other commands. When using the exec command in the current terminal, the specified command will replace the current process after execution, instead of creating a new child process. Command syntax exec [option] Option meaning Option meaning -c Use an empty environment to execute -ashell will pass the name as zero parameters to the executed command -l Place a dash at the beginning of the shell Parameters are passed to the command reference Example Example 1// First use the echo command to output the text "www.linuxyz.cn": [root@bunian~]#echoww

I believe that netizens are very familiar with the Windows 7 system. Have you heard of the Windows 7 English version system? I believe that many netizens have heard about the Windows 7 English version system. However, some friends are looking for the Windows 7 English version system to download. Today I will The editor is going to share the introduction of the original version of win7 in English with everyone, so that netizens can understand the original version of win7 in English. The following is to tell you where to download the English version of Windows 7 system. The original English system of win7 has been released to MSDN for subscription download. The official English integrated version was first released, Windows7WithSP1, which is the Windows7 CD image with integrated SP1. Includes SP1 standalone for multiple languages
