PHP exec () function introduction and use DEMO, execdemo
The exec () function is used to execute an external program. We can use this function in linux.
Enable the exec () function:
The exec () function is disabled. To use this function, you must enable it first. First, disable the security mode safe_mode = off. Then look at the disabled function list.
Disable_functions = proc_open, popen, exec, system, shell_exec, passthru
Remove exec and restart apache.
Basic usage of the exec () function:
Exec (string $ command [, array & $ output [, int & $ return_var]);
$ Command: indicates the command to be executed.
$ Output: If the output parameter is provided, the array is filled with the output of command execution, and each row is filled with an element in the array. The data in the array does not contain spaces at the end of the line, for example, \ n. Note that if the array already contains some elements, the exec () function will append content to the end of the array. If you do not want to append an array, use the unset () function to reset the array before passing in the exec () function.
$ Return_var: if both output and return_var parameters are provided, the return status after the command is executed is written to this variable.
In general, we only need to write the first parameter, that is, $ command.
Because the exec () function is mainly used to execute external programs, here we will take the linux system as an example to do a few demo Tutorials:
<?php
$command
=
"ls /tmp/test"
;
// Ls is the directory for querying files in linux.
exec
(
$command
,
$array
);
// Execute the command
print_r(
$array
);
?>
The returned results are as follows:
[root@krlcgcms01 shell]# php ./
exec
.php
Array
(
[0] => 1001.log
[1] => 10.log
[2] => 10.tar.gz
[3] => aaa.tar.gz
[4] => mytest
[5] => test1101
[6] => test1102
[7] => weblog_2010_09
)
My blog address: Introduction to PHP exec () function and use DEMO http://www.wangtuizhijia.com/archives/131