As a script language for servers, PHP is fully competent for tasks such as simple writing or complex dynamic web pages. But this is not always the case. sometimes, in order to achieve a certain effect, the external program (or
As a script language for servers, PHP is fully competent for tasks such as simple writing or complex dynamic web pages. But this is not always the case. sometimes, to achieve a certain effect, you must control the system's external programs (or commands) to get twice the result with half the effort.
Can an external command be called in a PHP script? If so, how can we do it? What are your concerns? After reading this article, I trust you to be able to reply to these headers.
Yes?
The answer is OK. Like other programming languages, PHP can completely call external commands in a program, and it is very simple: just use one or more functions.
Condition
Because PHP is used for WEB program development, security has become an important aspect of consideration. As a result, PHP designers added a security mode to PHP. If you run in safe mode, the PHP script will be subject to the following four restrictions:
Execute external commands
Restrictions on opening a file
Connect to the MySQL database
HTTP-based authentication
In security mode, only external programs in a specific directory can be executed, and calls to other programs will be rejected. This directory can be specified by using the safe_mode_exec_dir command in the php. ini file, or by adding the -- with-exec-dir option to compile PHP. the default value is/usr/local/php/bin.
If you call an external command that should be able to output results (that is, the PHP script is correct), the result is blank, it is very likely that your network administrator has run PHP in security mode.
How to do it?
You can use the following three methods to call external commands in PHP:
1) specialized functions provided by PHP
PHP provides a total of three specialized functions to execute external commands: system (), exec (), passthru ().
System ()
Prototype: string system (string command [, int return_var])
The system () function is a few differences among other languages. it executes the given commands, outputs, and returns results. The second parameter is optional and is used to obtain the status code after the command is executed.
Example:
System ('/usr/local/bin/webalizer ');
?>
Exec ()
Prototype: string exec (string command [, string array [, int return_var])
The exec () function is similar to system (). It also executes the given command, but does not output the result, but returns the last line of the result. Although it only returns the last line of the command result, the complete result can be obtained using the second parameter array by appending the result row by row to the end of the array. Therefore, if the array is not empty, we recommend that you use unset () to clear it before calling it. Only when the second parameter is specified can the third parameter be used to obtain the status code for command execution.
Example:
Exec ('/bin/ls-L ');
Exec ('/bin/ls-L', $ res );
# $ Res is a data. each element represents a row of results.
Exec ('/bin/ls-L', $ res, $ rc );
# $ Rc is the status code of the Command/bin/ls-l. The success is usually 0.
?>
Passthru ()