Four ways to execute system external commands using PHP

Source: Internet
Author: User
Tags array execution functions ini php file php web development php website linux

In the php file function on how to delete the file instance tutorial, I mentioned that there are two methods, one is to use the unlink function to delete a file, there is a system function to call the Systems command to execute the delete file command, in fact, in addition to the system function, PHP to execute the command of a variety Here are four ways to execute commands that are most commonly used in the development of PHP Web sites.

Preparatory work

For security reasons, in general, the PHP runtime environment, such as virtual host, XAMPP, Dedeampz, is prohibited from invoking system external commands. So when you need to invoke the system's external commands using the PHP command function, you need to make sure that the PHP runtime environment supports PHP's function of executing commands.

If you are using XAMPP, Dedeampz, the general default is to prohibit the operation of PHP execute command function, you can in the php.ini configuration file Disable_functions in an item to remove the function you want to use, and then restart Apache. I'm using Dedeampz, so I'm going to do four functions except EXEC (), PassThru (), System (), Shell_exec (). How does the PHP run environment configure?

If your PHP runtime environment is configurable, the default php.ini configuration file does not prohibit you from calling functions that execute external commands, but for security reasons, you should also disable the call to a function that executes the external command of the system, find the Disable_functions, and configure the following:

Disable_functions = Exec,system,passthru,shell_exec

This disables the execution of these three functions, separated by commas between each function. Php. INI How to configure?

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

Other Notes :

Since I am using a Windows system, the example tutorial is to invoke the external commands of Windows system, as you are familiar with Linux operations, you might as well try to execute Linux commands under Linux.

PHP four ways to execute commands

Method One: Use the EXEC function to execute system external commands

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

EXEC ("dir", $outPut);
Print_r ($outPut);

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

knowledge Point : Exec executes system external command does not output result, but returns the last line of result, if you want the result you can use the second parameter, let it output to the specified array, this array one record represents the output row, namely if the output result has 20 rows, Then this array has 20 records, so if you need to repeatedly output the results of calling different system external commands, you'd better empty the array to avoid clutter when outputting each system's external command results. The third parameter is used to obtain the status code for the execution of the command, which usually succeeds in returning 0.

Method Two: Use the system function to execute external commands

Prototypes: Function System (String $command, int[optional] $return _value)

System ("dir");

Knowledge points : System and exec differ in that system outputs the results directly to the viewer when executing the external commands of the systems, and returns False if the command succeeds. The second argument has the same meaning as the third argument of exec.

Method Three: Use function PassThru to execute system external command

Prototypes: Function PassThru (string $command, int[optional] $return _value)

Knowledge Point : The difference between PassThru and system, passthru directly output the results to the browser, does not return any value, and it can output binary, such as image data.

Method Four: Reverse apostrophe ' (and ~ in the same key) Execute system external command

Echo ' dir ';

Knowledge Point : When using this method to execute system external commands, you have to make sure that the Shell_exec function is available, otherwise you cannot use this counter apostrophe to execute system external commands.

Security instructions

When you use these functions to execute commands, you need to consider system security, using the Escapeshellcmd () and Escapeshellarg () functions to prevent users from maliciously executing commands on the system, depending on the user submitting the data as an execution command. Escapeshellcmd () is for the execution of system commands, while Escapeshellarg () is for the parameters that execute the system commands. These two parameters are somewhat similar to the Addslashes () feature.

Other Notes

When the return result of a command is very large, you may want to consider outputting the returned results to other files, and then reading the file separately, which can significantly increase the efficiency of program execution. That

System ("dir > Leapsoulcn.txt");

Note : This system executes the command without outputting the result directly to the viewer but to the file in the specified directory, which significantly improves the efficiency of the program execution.

The four most commonly used methods of executing commands here are exec (), PassThru (), System (), Shell_exec (), and the difference between them, and it is very useful to use these functions reasonably to execute system external commands in the development of PHP website.

  Note : PHP Web Development Copyright, reproduced in the form of links to indicate the original source and this statement, thank you.

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