Php 简明教程

PHP – System Calls

PHP 内置函数库包括一类函数,用于在 PHP 代码中调用操作系统实用程序和外部程序。在本章中,我们将讨论用于执行系统调用的 PHP 函数。

PHP’s library of built-in function includes a category of functions that deal with invoking operating system utilities and external programs from within the PHP code. In this chapter, we shall discuss the PHP functions used to perform system calls.

The system() Function

system() 函数类似于 C 语言中的 system() 函数,可执行给定的命令并输出结果。

The system() function is similar to the system() function in C that it executes the given command and outputs the result.

system(string $command, int &$result_code = null): string|false

如果 PHP 作为服务器模块运行,system() 调用会尝试在每行输出之后自动刷新 Web 服务器的输出缓冲区。成功时返回命令输出的最后一行,失败时返回 false。

The system() call tries to automatically flush the web server’s output buffer after each line of output if PHP is running as a server module. It returns the last line of the command output on success, and false on failure.

Example

下面的 PHP 代码调用 Windows 操作系统的 DIR 命令,并显示当前目录中的文件列表。

The following PHP snippet invokes DIR command of Windows OS and displays the list of files in the current directory.

<?php
   echo '<pre>';

   // Outputs all the result of DOS command "dir", and returns
   // the last output line into $last_line. Stores the return value
   // of the shell command in $retval.
   $last_line = system('dir/w', $retval);

   // Printing additional info
   echo '
   </pre>
   <hr />Last line of the output: ' . $last_line . '
   <hr />Return value: ' . $retval;
?>

它将生成以下 output

It will produce the following output

Volume in drive C has no label.
Volume Serial Number is 7EE4-E492

Directory of C:\xampp\htdocs
[.]                 [..]                applications.html   bitnami.css
[dashboard]         employee.csv        favicon.ico         hello.csv
hello.html          hello.php           homepage.php        [img]
index.php           [Langi]             menu.php            myform.php
myname.php          new.png             new.txt             test.php
test.zip            [TPcodes]           uploadfile.php      [webalizer]
welcome.png         [xampp]
                 18 File(s)          123,694 bytes
                 8 Dir(s)            168,514,232,320 bytes free

Last line of the output: 8 Dir(s) 168,514,232,320 bytes free
Return value: 0

The shell_exec() Function

shell_exec() 函数等同于 PHP 的反引号操作符。它通过 shell 执行给定命令,并以字符串的形式返回完整的输出。

The shell_exec() function is identical to PHP’s backtick operator. It executes the given command via shell and return the complete output as a string

shell_exec(string $command): string|false|null

函数返回一个包含已执行命令输出的字符串,如果管道不能建立,则返回 false;如果发生错误或命令没有产生输出,则返回 null。

The function returns a string containing the output from the executed command, false if the pipe cannot be established or null if an error occurs or the command produces no output.

Example

在下面的代码中,我们使用 shell_exec() 函数在当前目录中获取扩展名为 ".php" 的文件列表 −

In the following code, we use shell_exec() function to obtain a list of files with ".php" as the extension in the current directory −

<?php
   $output = shell_exec('dir *.php');
   echo "<pre>$output</pre>";
?>

它将生成以下 output

It will produce the following output

Volume in drive C has no label.
Volume Serial Number is 7EE4-E492

Directory of C:\xampp\htdocs

10/26/2023  08:27 PM                73 hello.php
10/12/2023  10:40 AM                61 homepage.php
07/16/2015  09:02 PM               260 index.php
10/12/2023  10:39 AM                49 menu.php
09/25/2023  01:43 PM               338 myform.php
10/12/2023  10:49 AM                51 myname.php
10/26/2023  02:00 PM               369 test.php
09/25/2023  01:42 PM               555 uploadfile.php
               8 File(s)          1,756 bytes
               0 Dir(s)           168,517,771,264 bytes free

The exec() Function

exec() 函数以字符串参数的形式执行给定的命令。

The exec() function executes the given command as a string argument.

exec(string $command, array &$output = null,
   int &$result_code = null):string|false

如果指定 $output 参数,它将是一个数组,其中填充了命令的每行输出。

The $output parameter, if specified, is an array that will be filled with every line of output from the command.

Example

在此情况下,我们使用 exec() 函数从程序内部调用 whoami 命令。whoami 命令返回用户名。

In this case, we use exec() function to call whoami command from inside the program. The whoami command returns the username.

<?php

   // outputs the username that owns the running php/httpd process
   // (on a system with the "whoami" executable in the path)
   $output=null;
   $retval=null;
   exec('whoami', $output, $retval);
   echo "Returned with status $retval and output:\n";
   var_dump($output);

?>

它将生成以下 output

It will produce the following output

Returned with status 0 and output: array(1)
{ [0]=> string(13) "gnvbgl3\mlath" }

The passthru() Function

passthru() 函数执行外部程序并显示原始输出。虽然 passthru() 函数类似于 exec() 或 system() 函数,因为它执行一个命令,但是当操作系统命令的输出是需要直接传回浏览器的二进制数据时,它应该用在它们的地方。

The passthru() function executes an external program and display raw output. Though the passthru() function is similar to the exec() or system() function in that it executes a command, it should be used in their place when the output from the OS command is binary data which needs to be passed directly back to the browser.

Example

一个使用 passthu() 函数显示系统 PATH 环境变量内容的 PHP 程序

A PHP program that uses passthu() function to display the contents of system PATH environment variable

passthru(string $command, int &$result_code = null): ?false
<?php
   passthru ('PATH');
?>

它将生成以下 output

It will produce the following output

PATH=C:\Python311\Scripts\;C:\Python311\;C:\WINDOWS\system32;C:\WINDOWS;
C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;
C:\WINDOWS\System32\OpenSSH\;C:\xampp\php;C:\Users\mlath\AppData\Local
\Microsoft\WindowsApps;C:\VSCode\Microsoft VS Code\bin

Backtick Operator

PHP 支持一个执行运算符:反引号 (``)。(它不是单引号!) PHP 会尝试将反引号的内容作为外壳命令执行;将返回输出。反引号运算符的使用与 shell_exec() 相同。

PHP supports one execution operator: backticks (``). (they are not single-quotes!) PHP will attempt to execute the contents of the backticks as a shell command; the output will be returned. Use of the backtick operator is identical to shell_exec().

Example

请看以下示例:

Take a look at the following example −

<?php
   $output = `dir *.php`;
   echo "<pre>$output</pre>";
?>

它将生成以下 output

It will produce the following output

Volume in drive C has no label.
Volume Serial Number is 7EE4-E492

Directory of C:\xampp\htdocs

10/26/2023  08:42 PM                61 hello.php
10/12/2023  10:40 AM                61 homepage.php
07/16/2015  09:02 PM               260 index.php
10/12/2023  10:39 AM                49 menu.php
09/25/2023  01:43 PM               338 myform.php
10/12/2023  10:49 AM                51 myname.php
10/26/2023  02:00 PM               369 test.php
09/25/2023  01:42 PM               555 uploadfile.php
               8 File(s)          1,744 bytes
               0 Dir(s)           168,471,289,856 bytes free

shell_exec() 被禁用时,反引号运算符将被禁用。

The backtick operator is disabled when shell_exec() is disabled.