Current Location: Home> Latest Articles>

M66 2025-07-10

PHP 函数:passthru

函数名

passthru

适用 PHP 版本

PHP 4, PHP 5, PHP 7, PHP 8

函数说明

passthru() 函数用于执行外部程序并显示原始输出,通常用于执行返回二进制数据的程序(如图像或音频流)。它将程序的原始输出直接传送到浏览器,不经过 PHP 的处理或缓冲。

函数语法

passthru(string $command, int &$result_code): void

参数

  • $command:要执行的命令。
  • &$result_code(可选):传入变量接收命令执行后的返回状态码。

返回值

没有返回值。如果需要获取命令的状态码,可通过第二个参数传入一个变量来获取。

示例

<?php
header("Content-Type: text/plain");
passthru("ls -l", $status);
echo "命令执行状态码: $status";
?>
  

示例代码的说明

上述示例中,PHP 使用 passthru 执行了 Unix/Linux 下的 ls -l 命令,并将输出直接发送到浏览器。header("Content-Type: text/plain") 用于确保浏览器将内容按纯文本显示。执行完命令后,命令的返回码被存储在变量 $status 中并被输出。