Four Methods for executing system external commands using PHP

Source: Internet
Author: User
In the php file function tutorial on how to delete file instances, I mentioned two methods: one is to use the unlink function to delete files, another method is to use the system function to call the system command to execute the delete File command. in the PHP file function tutorial on how to delete a file instance, I mentioned two methods, one is to use the unlink function to delete files, and the other is to use the system function to call system commands to execute file deletion commands. In fact, apart from the system function, there are many methods for PHP to execute commands, the following describes the four most common methods for executing commands in PHP website development.

Preparations

For the sake of security, PHP running environments such as virtual hosts, XAMPP, and DedeAMPZ are usually prohibited from calling system external commands. Therefore, when you need to use a function for executing commands in PHP to call external commands in the system, you need to ensure that the PHP runtime environment supports functions for executing commands in PHP.

If you are using XAMPP and DedeAMPZ, it is generally disabled to run the PHP command execution function by default. remove the functions you want from the disable_functions item in the ini configuration file and restart apache. I am using DedeAMPZ, so I will remove the exec (), passthru (), system (), shell_exec () functions. How to configure the PHP runtime environment?

If your PHP runtime environment is self-configured, default php. in the ini configuration file, you are not prohibited from calling functions that execute external commands. of course, for security reasons, you should still disable calling some functions that execute system external commands and find disable_functions. the configuration is as follows:

Disable_functions = exec, system, passthru, shell_exec

You are not allowed to execute these three functions. each function is separated by a comma. How to configure PHP. INI?

If you are using a virtual host, you need to ask the host provider if the PHP environment supports functions that use PHP to execute commands.

Other instructions:

Because I am using a Windows system, the instance tutorial focuses on calling external windows commands. if you are familiar with linux operations, you may try to execute linux commands in linux.

PHP command execution methods

Method 1: execute system external commands using the exec function

Prototype: function exec (string $ command, array [optional] $ output, int [optional] $ return_value)

1
2
3
4
Exec ("dir", $ outPut );
Print_r ($ outPut );
?>

Description: Lists all directories and file information under the same directory as the PHP execution file.

Knowledge Point: Exec does not output the result when executing the system external command, but returns the last line of the result. if you want to get the result, you can use the second parameter to output it to the specified array, A record in this array represents the output line. if there are 20 rows of output results, this array has 20 records. Therefore, if you need to repeatedly output the results of calling external commands of different systems, you 'd better clear the array when outputting the results of each system's external command to prevent confusion. The third parameter is used to obtain the status code for command execution. generally, 0 is returned for successful execution.

Method 2: use the system function to execute system external commands

Prototype: function system (string $ command, int [optional] $ return_value)

1
2
3
System ("dir ");
?>

Knowledge Point: The difference between system and exec is that system directly outputs the result to the browser when executing system external commands. if the command is successfully executed, true is returned; otherwise, false is returned. The second parameter has the same meaning as the third exec parameter.

Method 3: execute system external commands using the passthru function

Prototype: function passthru (string $ command, int [optional] $ return_value)

Knowledge Point: The difference between passthru and system. passthru directly outputs the result to the browser without returning any value. it can output binary data, such as image data.

Method 4: Anti-apostrophes (and ~ Execute system external commands with the same key.

1
2
3
Echo 'dir ';
?>

Knowledge Point: When using this method to execute system external commands, you must ensure that the shell_exec function is available; otherwise, the system external commands cannot be executed using this reverse code.

Security Description

When you use these functions to execute commands, if you use the data submitted by the user as the execution command, you need to consider system security, you can use escapeshellcmd () and escapeshellarg () the function prevents users from executing commands maliciously on the system. escapeshellcmd () is used to execute system commands, while escapeshellarg () is used to execute system command parameters. These two parameters are similar to the addslashes () function.

Other instructions

When the returned results of executing commands are very large, you can consider outputting the returned results to other files and then reading the files separately, which can significantly improve the efficiency of program execution. That is

1
2
3
System ("dir> leapsoulcn.txt ");
?>

Description: In this example, the system does not directly output the results to the browser but to the files in the specified directory, which significantly improves the program execution efficiency.

So far, the four most common methods for executing commands in PHP are exec (), passthru (), system (), shell_exec (), and their differences, in PHP website development, it is very useful to use these functions to execute system external commands as appropriate.

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.