passthru
PHP 4, PHP 5, PHP 7, PHP 8
passthru() 函数用于执行外部程序并显示原始输出,通常用于执行返回二进制数据的程序(如图像或音频流)。它将程序的原始输出直接传送到浏览器,不经过 PHP 的处理或缓冲。
passthru(string $command, int &$result_code): void
没有返回值。如果需要获取命令的状态码,可通过第二个参数传入一个变量来获取。
<?php header("Content-Type: text/plain"); passthru("ls -l", $status); echo "命令执行状态码: $status"; ?>
上述示例中,PHP 使用 passthru 执行了 Unix/Linux 下的 ls -l 命令,并将输出直接发送到浏览器。header("Content-Type: text/plain") 用于确保浏览器将内容按纯文本显示。执行完命令后,命令的返回码被存储在变量 $status 中并被输出。